对
python来说非常陌生,我想创建一个GUI窗口,我计划稍后将其用于查询工具。我一直收到全局名称根未定义错误。我正在尝试创建一个具有窗口的用户界面。我可能缺少对象概念。 需要一些帮助。谢谢。这是我的代码:
from Tkinter import *
class GUI(Frame):
"""A Graphical User Interface Class for building a GUI Frame.
Attributes:
master :
"""
def __init__(self,master):
Frame.__init__(self,master)
self.grid()
self.menubar()
self.label()
self.onExit()
def menubar(self):
self.menubar = Menu(root)
self.menubar.add_command(label="File")
self.menubar.add_command(label="Exit", command=self.onExit)
root.config(menu=self.menubar)
def label(self):
L0 = Label(text="Use this tool to find the lake location.").grid(row = 0, column = 0)
L1 = Label(text="Lake name :").grid(row = 2)
L2 = Label(text="County name :").grid(row = 3)
E0 = Entry().grid(row = 2, column = 2)
E1 = Entry().grid(row = 3, column = 2)
B0 = Button(text="Search").grid(row = 6, column = 2)
if self.E0 and self.E1==null:
raise RuntimeError('Entry cannot be blank.')
def onExit(self):
self.quit()
def main():
root = Tk()
root.geometry("400x200")
root.title(" Locator (L^3)")
app = GUI(root)
root.mainloop()
if __name__ == '__main__':
main()
def menubar(self):
self.menubar = Menu(root)
self.menubar.add_command(label="File")
self.menubar.add_command(label="Exit", command=self.onExit)
root.config(menu=self.menubar)
这里不存在root
。您要么需要root
全局变量(坏主意),要么将 root 传递给 GUI
类并保存对它的引用。
类似的东西
def __init__(self, master):
Frame.__init__(self, master)
self.master = master
self.grid()
self.menubar()
self.label()
self.onExit()
def menubar(self):
self.menubar = Menu(self.master)
self.menubar.add_command(label="File")
self.menubar.add_command(label="Exit", command=self.onExit)
self.master.config(menu=self.menubar)