実現したいこと
class win2で保存した値をwin1で使用したい
前提
class win2で保存した値をclass win1で使用できるようにしたいです。(win2のon_submitで保存した値をwin1のcalculate_white_ratioのthresholdに代入できるようにしたいです。)
ご教示のほどよろしくお願いいたします。
発生している問題・エラーメッセージ
line 28, in calculate_white_ratio
class2=Win2()
^^^^^^
TypeError: Win2.init() missing 1 required positional argument: 'master'
Python
1import tkinter as tk 2from tkinter import ttk 3import os.path 4from tkinter import filedialog 5import cv2 6class Win1(tk.Frame): 7 def __init__(self,master): 8 super().__init__(master) 9 self.pack() 10 11 self.master.geometry("400x400") 12 self.master.title("ホーム画面") 13 self.create_widgets() 14 15 def create_widgets(self): 16 # Button 17 self.setting_button = ttk.Button(self, text="設定", command=self.new_window) 18 self.setting_button.pack() 19 self.search_button = ttk.Button(self, text="画像を選択してください", command=self.calculate_white_ratio) 20 self.search_button.pack() 21 22 def calculate_white_ratio(self): 23 # 画像の読み込み 24 img_path = filedialog.askopenfilename() 25 img = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE) 26 27 # 二値化のしきい値 28 class2=Win2() 29 result = class2.on_submit() 30 threshold = result 31 32 # 二値化 33 _, binary_img = cv2.threshold(img, threshold, 255, cv2.THRESH_BINARY) 34 35 # 画像のサイズ 36 height, width = binary_img.shape[:2] 37 38 # 画素数 39 num_pixels = height * width 40 41 # 白色の画素数 42 white_pixels = cv2.countNonZero(binary_img) 43 44 # 黒色の画素数 45 black_pixels = num_pixels - white_pixels 46 47 # 白色の割合 (パーセント) 48 white_percent = (white_pixels / num_pixels) * 100 49 # 黒色の割合 (パーセント) 50 black_percent = (black_pixels / num_pixels) * 100 51 # 黒色の割合 (パーセント) 52 #black_percent = 100 - white_percent 53 54 self.label = ttk.Label(text='白の割合: {:.2f}%'.format(white_percent)) 55 self.label.pack() 56 self.label = ttk.Label(text='黒の割合: {:.2f}%'.format(black_percent)) 57 self.label.pack() 58 dst = cv2.resize(img, None, None, 0.1, 0.1) 59 cv2.imshow("image",dst) 60 61 #Call back function 62 def new_window(self): 63 self.newWindow = tk.Toplevel(self.master) 64 self.app = Win2(self.newWindow) 65 66class Win2(tk.Frame): 67 def __init__(self,master): 68 69 super().__init__(master) 70 self.pack() 71 self.master.geometry("300x300") 72 self.master.title("設定") 73 self.create_widgets() 74 75 def create_widgets(self): 76 # Button 77 self.value = 128 78 if os.path.exists("entry_value.txt"): 79 with open("entry_value.txt") as f: 80 self.value = int(f.read().strip()) 81 82 self.entry = ttk.Entry(self, validate="key", validatecommand=(self.register(self.validate_input), "%P")) 83 self.entry.insert(0, str(self.value)) 84 self.entry.pack() 85 86 self.submit = ttk.Button(self, text="保存", command=self.on_submit) 87 self.submit.pack() 88 self.submit = ttk.Button(self, text="終了", command=self.quit_window) 89 self.submit.pack() 90 91 self.label = ttk.Label(self, text="") 92 self.label.pack() 93 94 def validate_input(self, content): 95 return content.isdigit() 96 97 def on_submit(self): 98 value = self.entry.get() 99 with open("entry_value.txt", "w") as f: 100 f.write(value) 101 self.label.config(text="値が保存されました: {}".format(value)) 102 return value 103 104 def quit_window(self): 105 self.master.destroy() 106def main(): 107 root = tk.Tk() 108 app = Win1(master=root)#Inherit 109 app.mainloop() 110 111if __name__ == "__main__": 112 main()
回答1件