Python 使用带有按钮的模块的功能-Tkinter
所以我在学习模块。在使用了5个月左右后,我对tkinter很满意。如果我把我的函数放在我的主文件中,我可以让它工作。我将它们分为不同的模块,以便更好地学习如何使用模块。所以这个问题更多的是知识 我总共有3个文件,我的主循环(example_gui.py)、pythonic函数(example_funcs.py)、gui函数(more_example_funcs.py)。。。您可以看到,我的问题是在more_example_funcs.py中使用“get_text()”,这显然是为什么它在这种情况下不起作用。此.py文件中未定义该变量。我该怎么做?有人告诉我,将函数放在另一个文件(模块)中是更好的编码方式 对于使用Tkinter的全尺寸应用程序,我将有一组函数连接到条目,这些条目将在example_gui.py中定义。如果我可以像下面的示例一样,将这些函数放在更多example_funcs.py中,那就容易多了 示例_funcs.py 更多\u示例\u funcs.py 示例_gui.pyPython 使用带有按钮的模块的功能-Tkinter,python,python-2.7,tkinter,Python,Python 2.7,Tkinter,所以我在学习模块。在使用了5个月左右后,我对tkinter很满意。如果我把我的函数放在我的主文件中,我可以让它工作。我将它们分为不同的模块,以便更好地学习如何使用模块。所以这个问题更多的是知识 我总共有3个文件,我的主循环(example_gui.py)、pythonic函数(example_funcs.py)、gui函数(more_example_funcs.py)。。。您可以看到,我的问题是在more_example_funcs.py中使用“get_text()”,这显然是为什么它在这种情况
让您的
get_text
函数接受参数,以便以后可以对任何变量调用它
更多\u示例\u funcs.py
另外,在Page\u one
中使用self
关键字(self.entry\u one\u var
)将entry\u one\u var
作为类变量,因为您需要在多个方法中使用它,然后在调用get\u text
时将self.entry\u one\u var
作为参数传递
这就是Page\u one
类的外观:
class Page_one(tk.Frame):
def __init__(self, parent, controller, *args, **kwargs):
self.controller = controller
Frame.__init__(self, parent, *args, **kwargs)
self.labels_one()
self.buttons_one()
self.entries_one()
def labels_one(self):
label1 = Label(self, text="Welcome to page one")
label1.grid()
def buttons_one(self):
button_one = Button(self, text="go to page two", command=lambda:self.controller.show_frame(Page_two))
window_one_button = Button(self, text="open popup window", command=lambda:EF.popupmsg("New window 1"))
text_one_button = Button(self, text="print entered text", command=lambda: MEF.get_text(self.entry_one_var))##
button_one.grid()
window_one_button.grid()
text_one_button.grid()
def entries_one(self):
self.entry_one_var=StringVar() #make entry_one_var class instance variable
entry_one = Entry(self, textvariable= self.entry_one_var) ##
entry_one.grid()
我希望它能有所帮助。问题是什么?单击第1页()中的文本按钮将调用函数(获取文本)。。。我希望函数位于另一个文件中(更多示例\u funcs.py)。问题是,如何使该函数与example_gui.py中的输入字段一起工作?在函数中的某个时刻,我需要调用.get()来检索条目数据。这将需要访问example_gui.py中的变量。总之,你能让我的函数在文件中获取更多的文本吗。非常感谢。我最终找到了这个。。。。但是,您的代码解释得更好,特别是因为您使用了我的代码!哈哈。非常感谢。
from Tkinter import *
import Tkinter as tk
def get_text():
print entry_one_var.get()
from Tkinter import *
import Tkinter as tk
import example_funcs as EF
import more_example_funcs as MEF
class start(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *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 = {}
tk.Tk.title(self, "app name")
menubar = tk.Menu(container)
tk.Tk.config(self, menu=menubar)
fileMenu = tk.Menu(menubar, tearoff=0)
menubar.add_cascade(label="File", menu=fileMenu)
fileMenu.add_command(label="Exit", command=quit)
for F in (Page_one, Page_two, Page_three):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(Page_one)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class Page_one(tk.Frame):
def __init__(self, parent, controller, *args, **kwargs):
self.controller = controller
Frame.__init__(self, parent, *args, **kwargs)
self.labels_one()
self.buttons_one()
self.entries_one()
def labels_one(self):
label1 = Label(self, text="Welcome to page one")
label1.grid()
def buttons_one(self):
button_one = Button(self, text="go to page two", command=lambda:self.controller.show_frame(Page_two))
window_one_button = Button(self, text="open popup window", command=lambda:EF.popupmsg("New window 1"))
text_one_button = Button(self, text="print entered text", command=MEF.get_text)
button_one.grid()
window_one_button.grid()
text_one_button.grid()
def entries_one(self):
entry_one_var=StringVar()
entry_one = Entry(self, textvariable= entry_one_var)
entry_one.grid()
class Page_two(tk.Frame):
def __init__(self, parent, controller, *args, **kwargs):
self.controller = controller
Frame.__init__(self, parent, *args, **kwargs)
self.labels_two()
self.buttons_two()
def labels_two(self):
label2 = Label(self, text="Welcome to page two")
label2.grid()
def buttons_two(self):
button_two = Button(self, text="go to page three", command=lambda:self.controller.show_frame(Page_three))
window_two_button = Button(self, text="open popup window", command=lambda:EF.popupmsg("New window 2"))
button_two.grid()
window_two_button.grid()
class Page_three(tk.Frame):
def __init__(self, parent, controller, *args, **kwargs):
self.controller = controller
Frame.__init__(self, parent, *args, **kwargs)
self.labels_three()
self.buttons_three()
def labels_three(self):
label3 = Label(self, text="Welcome to page three")
label3.grid()
def buttons_three(self):
button_three = Button(self, text="go to page one", command=lambda:self.controller.show_frame(Page_one))
window_three_button = Button(self, text="open popup window", command=lambda:EF.popupmsg("New window 3"))
button_three.grid()
window_three_button.grid()
app = start()
EF.center(app)
app.mainloop()
from Tkinter import *
import Tkinter as tk
def get_text(var):
print var.get()
class Page_one(tk.Frame):
def __init__(self, parent, controller, *args, **kwargs):
self.controller = controller
Frame.__init__(self, parent, *args, **kwargs)
self.labels_one()
self.buttons_one()
self.entries_one()
def labels_one(self):
label1 = Label(self, text="Welcome to page one")
label1.grid()
def buttons_one(self):
button_one = Button(self, text="go to page two", command=lambda:self.controller.show_frame(Page_two))
window_one_button = Button(self, text="open popup window", command=lambda:EF.popupmsg("New window 1"))
text_one_button = Button(self, text="print entered text", command=lambda: MEF.get_text(self.entry_one_var))##
button_one.grid()
window_one_button.grid()
text_one_button.grid()
def entries_one(self):
self.entry_one_var=StringVar() #make entry_one_var class instance variable
entry_one = Entry(self, textvariable= self.entry_one_var) ##
entry_one.grid()