我收到以下错误:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python33\lib\tkinter\__init__.py", line 1475, in __call__
return self.func(*args)
File "D:\COMPUTER SCIENCE\Seating Plan\SeatingPlan TEST.py", line 205, in displayText
if roomChange.get().strip() == "":
NameError: global name 'roomChange' is not defined
尝试运行以下代码时:
from tkinter import *
import tkinter.messagebox
def displayText():
""" Display the Entry text value. """
global roomChange
if roomChange.get().strip() == "":
tkinter.messagebox.showerror("Invalid Value", "Please enter a valid classroom name.")
else:
tkinter.messagebox.showinfo("Temporary Window", "Text value = " + roomChange.get().strip())
def roomChanger():
chrm = Tk()
chrm.title("Change Room")
chrm.wm_iconbitmap('./Includes/icon.ico')
chrm["padx"] = 40
chrm["pady"] = 20
# Create a text frame to hold the text Label and the Entry widget
textFrame = Frame(chrm)
#Create a Label in textFrame
roomChangeLabel = Label(textFrame)
roomChangeLabel["text"] = "Enter name of classroom: "
roomChangeLabel.pack(side=LEFT)
# Create an Entry Widget in textFrame
roomChange = Entry(textFrame)
roomChange["width"] = 50
roomChange.pack(side=LEFT)
textFrame.pack()
roomChangeButton = Button(chrm, text="Submit", command=displayText)
roomChangeButton.pack()
chrm.mainloop()
testButton = Button(window, text='Change Room', command=roomChanger)
testButton.place(x = 825, y = 360)
有人能建议解决我的问题吗? 感谢
答案 0 :(得分:1)
roomChanger()
roomChange
分配给roomChange = Entry(textFrame)
:
global roomChange
所以你需要将该名称标记为该函数的全局内容。在该函数中添加displayText()
语句。
roomChange
永远不会尝试分配给global
,并且可以安全地删除该函数中的{{1}}语句。
答案 1 :(得分:-2)
我遇到了同样的问题。
这是我的解决方案:
from tkinter import *
from tkinter import messagebox
某种命名空间故障。第二行不应该是必要的。从技术上讲,从语法角度来看import *
也意味着导入messagebox
,因为它是它的一部分。
使用这两行,带走import tkinter.messagebox