tkinterで画像クリックする際にOSErrorについて

前提

tkinterを使って画像クリックをしたいですが、
下記のOSErrorエラーが表示されます。
エラーの原因の解決方法分かりますでしょうか。

実現したいこと

①画像取得>保存
②フォルダーで保存した画像を選択
③画像クリック

試したこと

画像クリックの関数def click_image():
#positionでコメントアウトしている部分で行うと問題なく
クリックできますが変数にするとOSErrorエラーが表示されます。

selected_fileの関数からclick_imageの関数にselected_fileを入れるとエラーが発生します。

selected_file = "" #ファイル参照、ボタンがクリックされたら実行 def file_select(): idir = 'C:\\python_test' #初期フォルダ filetype = [("すべて","*"),("テキスト","*.txt"), ("音楽","*.mp3")] #拡張子の選択 file_path = tk.filedialog.askopenfilename(filetypes = filetype, initialdir = idir) selected_file = file_path.replace('/','//') input_box.insert(tk.END, file_path) #結果を表示 print(selected_file)

変数なしの場合の画像ファイルパスと同じパスはselected_fileにも出力されます。
print(selected_file)

C://Users//test//Documents//image_test//excel.PNG
#画像クリック、ボタンがクリックされたら実行 def click_image(): global selected_file # #待機時間3秒 # time.sleep(3) #ファイル参照の関数からfile_pathを代入、このPCアイコンの座標を取得 position=pyautogui.locateOnScreen(selected_file, confidence=0.9) #position=pyautogui.locateOnScreen("C://Users//test//Documents//image_test//excel.PNG", confidence=0.9) #maxwindowPCアイコンをクリック pyautogui.doubleClick(position)

発生している問題・エラーメッセージ

OSError: Failed to read because file is missing, has improper permissions, or is an unsupported or invalid format

該当のソースコード

import tkinter as tk from PIL import Image, ImageTk import pyautogui import time import subprocess import os from tkinter import filedialog import re #SnippingTool起動、ボタンがクリックされたら実行 def image(event): p = subprocess.Popen([r"C:\Windows\System32\SnippingTool.exe"]) selected_file = "" #ファイル参照、ボタンがクリックされたら実行 def file_select(): idir = 'C:\\python_test' #初期フォルダ filetype = [("すべて","*"),("テキスト","*.txt"), ("音楽","*.mp3")] #拡張子の選択 file_path = tk.filedialog.askopenfilename(filetypes = filetype, initialdir = idir) selected_file = file_path.replace('/','//') input_box.insert(tk.END, file_path) #結果を表示 print(selected_file) #画像クリック、ボタンがクリックされたら実行 def click_image(): global selected_file # #待機時間3秒 # time.sleep(3) #ファイル参照の関数からfile_pathを代入、このPCアイコンの座標を取得 position=pyautogui.locateOnScreen(selected_file, confidence=0.9) #position=pyautogui.locateOnScreen("C://Users//test//Documents//image_test//excel.PNG", confidence=0.9) #maxwindowPCアイコンをクリック pyautogui.doubleClick(position) # 画面作成 window = tk.Tk() window.geometry("300x300") window.title("ボタンを表示する") #画像取得 ボタン作成 btn1 = tk.Button(window, text="画像取得") # ボタン表示 btn1.place(x=15, y=15, width=150, height=40) # ボタンに関数をbind btn1.bind("<Button-1>", image) #入力欄の作成 input_box = tk.Entry(width=40) input_box.place(x=10, y=100) #結果ラベルの作成 input_label = tk.Label(text="結果") input_label.place(x=10, y=70) #参照ボタンの作成 button = tk.Button(text="参照",command=file_select) button.place(x=10, y=130) #画像クリックボタンの作成 button = tk.Button(text="画像クリック",command=click_image) button.place(x=15, y=175, width=150, height=40) # # ボタン作成 # btn2 = tk.Button(window, text="画像クリック") # # ボタン表示 # btn2.place(x=15, y=175, width=150, height=40) # # ボタンに関数をbind # btn2.bind("<Button-2>", clik_image) # 画面表示(常駐) window.mainloop()

お手数ですが、よろしくお願い致します。

コメントを投稿

0 コメント