當類“Rightframe”中的函式“chk_file_exist”呼叫另一個函式時出現錯誤訊息
Leftframe().update_listbox()###error TypeError: init () missing 1 require d positional argument: 'parent' TypeError: init () missing 1 required positional argument: 'parent'
GUI Image:Leftframe 有一個串列框,Rightframe 有兩個按鈕(開始和停止)

代碼層:
1.功能empty_listbox類Leftframe產生空串列框
2.用戶在Rightframe中按start_btn(另一個腳本開始生成一個檔案'XXX.pkl')
3.function chk_file_exist類Rightframe會一直等待,直到“XXX.pkl”存在(我有強迫這個條件是真用于除錯)
4.function chk_file_exist類Rightframe呼叫函式update_listbox類Leftframe決議“XXX.pkl”,...(發生錯誤)
我的代碼:
import tkinter as tk
import os
class Leftframe(tk.Frame):
def __init__(self, parent, *args, **kwargs):
tk.Frame.__init__(self, parent, *args, **kwargs)
self.parent = parent
self.scrollbar = tk.Scrollbar(self)
self.scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
self.mylist = tk.Listbox(self, yscrollcommand=self.scrollbar.set)
self.mylist.place(x=0, y=0, width=260, height=410)
self.scrollbar.config(command=self.mylist.yview)
self.empty_listbox()
def empty_listbox(self):
self.mylist.delete(0, tk.END)
for it in ([' ']):
self.mylist.insert(tk.END, it)
def update_listbox(self):
self.mylist.delete(0, tk.END)
#do something ... parsed a file, updates listbox
#for it in parsedlist:
# self.mylist.insert(tk.END, it)
class Rightframe(tk.Frame):
def __init__(self, parent, *args, **kwargs):
tk.Frame.__init__(self, parent, *args, **kwargs)
self.parent = parent
self.start_btn = tk.Button(self,text='Start')
self.start_btn.config(command=self.button_start_push)
self.start_btn.place(anchor=tk.NW,x=10,y=10,width=60,height=20)
self.stop_btn = tk.Button(self,text='Stop')
self.stop_btn['state'] = tk.DISABLED
self.stop_btn.config(command=self.button_stop_push)
self.stop_btn.place(anchor=tk.NW,x=10,y=60,width=60,height=20)
def chk_file_exist(self):
if 1:#os.path.isfile(os.path.join(os.curdir, fileroute)): force for debugging!!
Leftframe().update_listbox()###error TypeError: __init__() missing 1 required positional argument: 'parent'
else:
print('file generating, please wait')#other script generating file in background
main_win.after(1000, self.chk_file_exist)
def button_start_push(self):
print('Started')
self.start_btn['state'] = tk.DISABLED
self.stop_btn['state'] = tk.NORMAL
global fileroute
self.chk_file_exist()
def button_stop_push(self):
print('stop button pressed')
self.start_btn['state'] = tk.NORMAL
open(os.path.join(os.curdir, 'stop.txt'), 'a').close()
if __name__ == '__main__':
fileroute = 'XXX.pkl'
main_win = tk.Tk()
main_win.geometry("200x150 0 0")
Leftframe(main_win, relief="sunken", borderwidth=1).place(x=10, y=10, width=80, height=100)
Rightframe(main_win, relief="sunken", borderwidth=1).place(x=100, y=10, width=80, height=100)
main_win.mainloop()
我正在嘗試使我的代碼像下面的這個鏈接一樣面向物件 構建 tkinter 應用程式的最佳方法? 我應該使用視窗作為類而不是框架嗎?還是應該將所有元素(串列框、按鈕、條目...)放入一個視窗類中,而不是使用框架?
我很感激你的所有建議
非常感謝!!
uj5u.com熱心網友回復:
您沒有為Leftframe().update_listbox()function 中的行添加父小部件引數chk_file_exist(self):,因此添加self.parent(即main_win) -
Leftframe(self.parent).update_listbox()
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/360444.html
