Sergey
Sergey

Reputation: 1001

how to open a child window?

please help to fix the script.

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()

after clicking on the button "press me" should open a child window. but the console displays an error message:

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

Upvotes: 0

Views: 553

Answers (1)

user2555451
user2555451

Reputation:

You imported the name tkinter which contains the class Label. Meaning, in order to access it, you need to put tkinter. before it (just like you did for Frame, Button, etc.):

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

Otherwise, Python will not know where Label is defined.

Upvotes: 2

Related Questions