我会简短。我创建了一个tkinter GUI,需要将其作为对象或单击按钮时出现的窗口进行调用。 代码是:
class Test(Frame):
def __init__(self,master = None):
Frame.__init__(self, master)
self.master = master
self.win()
def win(self):
self.pack()
self.label = Label(self, text="hello World!").pack()
self.quit = Button(self, text= "quit", command = self.master.destroy).pack()
当我在同一个文件中调用类时,代码工作正常。即,通过添加
root=Tk()
Test()
但我希望在单击按钮时调用它,但在其他GUI中调用它。
我尝试过的方法:
1)照原样应用相同的root = Tk()
。
2)通过以下方式在类中将其作为对象调用:self.test = Test()并在按钮中应用命令self.test.win。
问题:“模块”对象不可调用。
其他gui的代码,我希望按钮调用Test类并显示该类的gui:
import Test
class Wtf(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.master = master
self.btn()
self.test = Test()
def btn(self):
self.pack()
self.test = Test()
self.btn = Button(self, text = "Call", command = self.test.win).pack()
root=Tk()
app = Wtf(root)
先谢谢了。希望我能按要求进行定义。
对于那些不了解的人:我要在这里做的就是将类数据链接到按钮“ btn”,这样当我按下按钮时,我就可以得到类Test gui显示在相同的窗口根目录或不同的窗口中。
请注意::我是python的新手,该程序可能对您没有意义,但是我在这里尝试的只是在按钮上单击“ btn”调用Test类
答案 0 :(得分:0)
我想我知道,在此版本中,您可以从程序的主类中调用Test()类中的函数。
运行脚本后,单击“打开”按钮,然后单击“呼叫”并观察发生的情况。
import tkinter as tk
from tkinter import messagebox
class Test(tk.Toplevel):
def __init__(self, parent):
super().__init__()
self.parent = parent
self.title("I'm a new toplevel.")
self.init_ui()
def init_ui(self):
self.label = tk.Label(self, text=self.title()).pack()
self.close_me = tk.Button(self, text= "Close me", command = self.on_close_me).pack()
self.close_parent = tk.Button(self, text= "Close parent", command = self.on_close_parent).pack()
def on_close_me(self):
self.destroy()
def on_close_parent(self):
self.parent.on_close()
def callback(self):
msg = "I come from parent toplevel"
messagebox.showwarning(self.master.title(), msg, parent=self)
class App(tk.Frame):
def __init__(self,):
super().__init__()
self.master.title("Hello World")
self.obj = None
self.init_ui()
def init_ui(self):
self.pack(fill=tk.BOTH, expand=1,)
f = tk.Frame()
w = tk.Frame()
tk.Button(w, text="Open", command=self.callback).pack()
tk.Button(w, text="Call", command=self.call_child_function).pack()
tk.Button(w, text="Close", command=self.on_close).pack()
f.pack(side=tk.LEFT, fill=tk.BOTH, expand=0)
w.pack(side=tk.RIGHT, fill=tk.BOTH, expand=0)
def callback(self):
self.obj = Test(self)
def call_child_function(self):
self.obj.callback()
def on_close(self,evt=None):
self.master.destroy()
if __name__ == '__main__':
app = App()
app.mainloop()
答案 1 :(得分:0)
我不完全了解您要完成的工作,但是您的代码存在很多问题,其中一些我已经在注释中指出。
'module' object is not callable
问题是因为您有一个名为Test
的模块,该模块在其中定义了一个名为Test
的类,因此在引用时可以使用Test.Test
来避免此问题。上课。
以下是主脚本和Test.py
模块的工作版本:
main.py
:
from tkinter import *
import Test
class Wtf(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.master = master
self.pack()
self.create_widgets()
def create_widgets(self):
self.test = Test.Test(self.master)
self.btn = Button(self, text="Call", command=self.test.win)
self.btn.pack()
root = k()
app = Wtf(root)
app.mainloop()
Test.py
:
from tkinter import *
class Test(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.master = master
self.pack()
def win(self):
self.label = Label(self, text="Hello World!")
self.label.pack()
self.quit = Button(self, text= "quit", command=self.master.destroy)
self.quit.pack()
更新
根据您对您真正想做的事情的评论,我针对您的需求对问题accepted answer修改了Switch between two frames in tkinter:
main.py
import tkinter as tk
import Test
class App(tk.Tk):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
container = tk.Frame(self)
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (Wtf, Test.Test):
page_name = F.__name__
frame = F(parent=container, controller=self)
self.frames[page_name] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame("Wtf")
def show_frame(self, page_name):
""" Show a frame for the given page name. """
frame = self.frames[page_name]
frame.tkraise()
class Wtf(tk.Frame):
def __init__(self, parent, controller):
super().__init__(parent)
self.parent = parent
self.controller = controller
self.create_widgets()
def create_widgets(self):
self.test = Test.Test(self.parent, self.controller)
self.btn = tk.Button(self, text="Call",
command=lambda: self.controller.show_frame("Test"))
self.btn.pack()
if __name__ == "__main__":
app = App()
app.mainloop()
Test.py
:
import tkinter as tk
class Test(tk.Frame):
def __init__(self, parent, controller):
super().__init__(parent)
self.parent = parent
self.controller = controller
self.create_widgets()
def create_widgets(self):
self.label = tk.Label(self, text="Hello World!")
self.label.pack()
self.quit = tk.Button(self, text= "Quit", command=self.controller.destroy)
self.quit.pack()
答案 2 :(得分:-1)
无论如何,我不确定该问题是否已经理解。
尤其是
self.close_me = tk.Button(self,text =“ Close me”,command = self.on_close_me).pack()
self.close_parent = tk.Button(self,text =“亲密父母”,命令= self.on_close_parent).pack()
您甚至可以将Test类放到另一个文件上并输入。
import tkinter as tk
class Test(tk.Toplevel):
def __init__(self, parent):
super().__init__()
self.parent = parent
self.title("I'm a new toplevel.")
self.init_ui()
def init_ui(self):
self.label = tk.Label(self, text=self.title()).pack()
self.close_me = tk.Button(self, text= "Close me", command = self.on_close_me).pack()
self.close_parent = tk.Button(self, text= "Close parent", command = self.on_close_parent).pack()
def on_close_me(self):
self.destroy()
def on_close_parent(self):
self.parent.on_close()
class App(tk.Frame):
def __init__(self,):
super().__init__()
self.master.title("Hello World")
self.init_ui()
def init_ui(self):
self.pack(fill=tk.BOTH, expand=1,)
f = tk.Frame()
w = tk.Frame()
tk.Button(w, text="Open", command=self.callback).pack()
tk.Button(w, text="Close", command=self.on_close).pack()
f.pack(side=tk.LEFT, fill=tk.BOTH, expand=0)
w.pack(side=tk.RIGHT, fill=tk.BOTH, expand=0)
def callback(self):
obj = Test(self)
def on_close(self,evt=None):
self.master.destroy()
if __name__ == '__main__':
app = App()
app.mainloop()