Tkinter代码运行但不显示窗口

时间:2018-05-22 14:04:06

标签: python tkinter

我注意到如果我删除/注释下面的代码对应名称###########部分的###########创建下拉列表,则会出现该窗口。此外,如果我在不同的文件中使用相同的代码,DropDown列表显示为正常....超级怪异!! 不知道这里发生了什么。 你能帮我吗? 代码如下:

from tkinter import *       

###########  Create main window, call it root  ###########
root = Tk()

#Defining classes and methods
class Code:

    def __init__(self, code, name, skill, description):
        self.code = code
        self.name = name
        self.skill = skill
        self.description = description

        codesList.append(self)
        namesList.append(name)

#Defining variables
codesList = []
namesList = []

selectedname = StringVar()
selectedname.set('Please select the correct the correct knowledge category necessary to resolve the ticket.')

A11 = Code(11, 'Fundamentals', 'A', '')

B11 = Code(11,'Geology', 'B', '')
B12 = Code(12,'Reservoir Engineering', 'B', '')

###########  Create list of Dictionaries  ###########     
TypeD = {}
SiteD = {}
P4dcD = {}
SkillD = {}


###########  Create Incident Types  Dictionary###########
TypeD = dict(Normal='NORMAL', \
             Parent='PARENT', \
             Child='CHILD')
TypeNameL = list()
for i in TypeD.keys():
    TypeNameL.append(i)                                        


###########  Create Skill Levels  ###########
SkillD = dict(Awareness = 'A', \
              Foundation = 'B', \
              Skill = 'C', \
              Advance = 'D')
SkillNameL = list()
for i in SkillD.keys():
    SkillNameL.append(i)

###########  Create main menu  ########### 
mainmenu = Menu(root)
root.config(menu = mainmenu)
submenu = Menu(mainmenu)

mainmenu.add_cascade(label = "File", menu=submenu)
submenu.add_command(label = "New Project", command=doNothing)         
submenu.add_separator()
submenu.add_command(label = "Exit", command= exitmeth)

editmenu = Menu(mainmenu)
mainmenu.add_cascade(label = "Edit", menu=editmenu)
editmenu.add_command(label = "Redo", command = doNothing)

###########  Toolbar  ###########
toolbar = Frame(root, bg="blue")                                                
insertButt = Button(toolbar, text = "Insert image", command = InsertImgcmd)      
insertButt.pack(side=LEFT, padx = 2, pady=2)                                    
printButt = Button(toolbar, text="Print", command = printP4dcD)
printButt.pack(side=LEFT, padx = 2, pady=2)

toolbar.pack(side=TOP, fill=X)                                              

###########  Create Dropdown list for Code corresponding name ###########
LabelCodeDDL = Label(root, text="Petrel Code").grid(row=2, column=0, sticky=W)
CodeDDL = OptionMenu(root, selectedname, *namesList)
CodeDDL.pack()
CodeDDL.configure(font=("Calibri", 11))
CodeDDL.grid(row=3, column=0)


##########  Statusbar  ##########
statusbar = Label(root, text='', bd=1, relief=SUNKEN, anchor=W)
statusbar.pack(side=BOTTOM, fill=X)                                        

root.title("Ticket Summary Tool")
root.geometry("300x200")
root.mainloop()

我应该得到一些错误或者应该显示窗口。我无法弄清楚发生了什么......

1 个答案:

答案 0 :(得分:1)

您正在将packgrid用于直接位于root的小部件(例如,LabelCodeDDL = Label(root, text="Petrel Code").grid(row=2, column=0, sticky=W)toolbar.pack(side=TOP, fill=X))。你不能这样混合它们。具有相同父级或母版的所有窗口小部件必须使用其中一个或另一个。