我有一个超类(tk窗口)和另一个继承原始类属性的子类(tk窗口)。在超类上,我有一个方法callback
每秒调用一次。子类显示一个tk选项菜单,我想通过每秒调用的超类方法更新tk选项菜单中显示的选项。我已经创建了一个缩短版的程序,它通常显示了我正在尝试做的事情。这个程序将运行,它按照我的意愿运行,但是在pycharm中我收到一个突出显示子类方法def update(self, controller):
的警告:
Signature of method 'OtherPage.update()' does not match
signature of base method in class 'Misc'
这样做的正确方法是什么?
import tkinter as tk
exchanges = ['Bitfinex', 'Bittrex', 'Kraken', 'Gdax']
class Trader(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
tk.Tk.wm_title(self, "Multi-Trader-Wallet")
container = tk.Frame(self)
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
menubar = tk.Menu(container)
filemenu = tk.Menu(menubar, tearoff=0)
filemenu.add_command(label='Exit', command=lambda: print('exit'))
menubar.add_cascade(label='File', menu=filemenu)
tk.Tk.config(self, menu=menubar)
self.exchange1 = tk.StringVar()
self.frames = {}
self.id = self.after(1000, self.callback)
for F in (MainPage, OtherPage):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, columnspan=1, rowspan=1, sticky="nsew")
self.show_frame(OtherPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
def callback(self):
print('callback')
print(self.frames[OtherPage])
self.frames[OtherPage].update(self)
self.id = self.after(1000, self.callback)
class MainPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text='MainPage')
label.grid(row=1, column=1)
class OtherPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label2 = tk.Label(self, text='OtherPage')
label2.grid(row=1, column=1)
drop = tk.OptionMenu(self, controller.exchange1, *exchanges)
drop.config(width=10)
drop.grid(row=2, column=1)
def update(self, controller):
exchanges.append('new exchange')
drop = tk.OptionMenu(self, controller.exchange1, *exchanges)
drop.config(width=10)
drop.grid(row=2, column=1)
app = Trader()
app.mainloop()