如何打开一个儿童窗口?

时间:2014-01-31 17:25:04

标签: python tkinter

请帮助修复脚本。

import tkinter

def winMake(parent):
    win = tkinter.Frame(parent)
    win.config(relief = 'sunken', width = 340, height = 170, bg = 'red')
    win.pack(expand = 'yes', fill = 'both')

    msg = tkinter.Button(win, text='press me', command = addFormOpen)
    msg.pack()

def addFormOpen():
    addForm = tkinter.Toplevel(root)
    Label(addForm, text = 'ertert').pack()
    print('fff')

root = tkinter.Tk()
winMake(root)
root.mainloop()

点击“按我”按钮后应打开一个子窗口。但控制台显示错误消息:

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 "C:\Python33\projects\DVD_LIS\p3_dvd_list_shelve_3d_class_edit_menubar\q.py", line 13, in addFormOpen
    Label(addForm, text = 'ertert').pack() 
NameError: global name 'Label' is not defined

1 个答案:

答案 0 :(得分:2)

您导入的名称tkinter 包含Label。这意味着,为了访问它,您需要将tkinter.放在它之前(就像您对FrameButton等所做的那样):

tkinter.Label(addForm, text = 'ertert').pack()

否则,Python将不知道Label的定义位置。