我想使用Tkinter的創建GUI,代碼:NameError:名字「頂」沒有定義
from tkinter import *
class LoginFrame(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.initUI()
# initialize the login screen UI
def initUI(self):
self.parent.title("Login Screen")
# create a menu bar
menubar = Menu(top)
# create a help menu
helpmenu = Menu(menubar, tearoff=0)
helpmenu.add_command(label="About", command=about)
menubar.add_cascade(label="Help", menu=helpmenu)
# display the menu
self.parent.config(menu=menubar)
#----------------------------------------------------------------------
def about():
"""about info"""
print("This is a Tkinter demo")
# create a button
#----------------------------------------------------------------------
def make_button(parent, command, caption=NONE, side=top, width=0, **options): # name error 'top' is not defined
"""make a button"""
btn = Button(parent, text=caption, command=command)
if side != top:
btn.pack(side=side)
else:
btn.pack()
return btn
def main():
top = Tk()
# Set up login frame properties
top.title("Login Screen")
# create a login button
login_btn = make_button(top, about, "Login")
top.mainloop()
if __name__ == '__main__':
main()
我試圖運行代碼,蟒蛇給了我以下錯誤:
builtins.NameError: name 'top' is not defined
但是,頂部是在tkinter中定義的,我已經導入tkinter – daiyue
@daiyue:是嗎?在Python 3.5終端中,我只是做了'import tkinter','>>> tkinter.top',並且給了一個'AttributeError'。 – ShadowRanger
我不認爲這是在我的發行版中 - 嘗試從'tkinter import top'輸入到您的解釋器中,我認爲它會失敗。您正在定義主函數中的top,但它在本地範圍內 - 即它只能用於該函數。 – srowland