-1

我收到以下错误:

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)

谁能建议我的问题的解决方案?谢谢

4

2 回答 2

1

roomChanger()您分配给roomChange

roomChange = Entry(textFrame)

因此,您还需要将该名称标记为该函数内的全局名称。global roomChange在该函数中添加一条语句。

displayText()另一方面,从不尝试分配给,roomChange并且global可以安全地删除该函数中的语句。

于 2013-09-21T21:48:37.190 回答
-2

我有同样的问题。

这是我的解决方案:

from tkinter import * 
from tkinter import messagebox

某种命名空间故障。第二行不应该是必要的。从语法的角度来看,技术上也import *意味着导入messagebox,因为它是其中的一部分。

使用那两条线,带走import tkinter.messagebox

于 2014-01-12T16:09:13.410 回答