2013-03-25 36 views
0

我想出了以下代碼作爲參考,以幫助我記住如何使用TkInter構建GUI應用程序。除非單擊Button1或其命令選項設置爲self.hello的其他窗口小部件,否則它運行良好。正如你在代碼中看到的那樣,hello函數就像一個佔位符。雖然通過IDLE運行腳本時,按鈕單擊工作正常,但如果通過雙擊實際文件test.pyw啓動程序,它只會導致應用程序退出。我的問題是,爲什麼?Python 3.3 TkInter窗口意外關閉。爲什麼?

#Some guy somewhere 

from tkinter import * 

class Application: 

    def hello(self): 
     msg = messagebox.showinfo('Message Title','Message Body') 

    def __init__(self, parent): 
     parent.resizable(0,0) 
     parent.minsize(800, 400) 
     parent.title('Top Level') 

     # Global Padding pady and padx 
     pad_x = 0 
     pad_y = 0 


     # CASCADE MENU 
     # create a parent menu. 
     self.parentmenu1 = Menu(parent, tearoff=0) 
     #self.menubar1.add_command(label='Menu1', command=self.hello) 

     #create a child menu for parent menu. 
     self.parentmenu1_child1 = Menu(parent, tearoff=0) 
     self.parentmenu1_child1.add_command(label='Item1', command=self.hello) 
     self.parentmenu1_child1.add_command(label='Item2', command=self.hello) 
     self.parentmenu1_child1.add_command(label='Item3', command=self.hello) 

     #add child menu to parent menu. 
     self.parentmenu1.add_cascade(label='Menu1', menu=self.parentmenu1_child1) 

     #self.menubar1.add_separator() 

     # SINGLE MENU 
     # create a parent menu. 
     self.parentmenu1.add_command(label='Menu2', command=self.hello) 

     # SINGLE MENU 
     # create a parent menu. 
     self.parentmenu1.add_command(label='Menu3', command=self.hello) 

     # display the parent menu. 
     parent.config(menu=self.parentmenu1) 

     # Create controls 

     #create label 
     self.label1 = Label(parent, text='Label1') 
     #create textbox 
     self.textbox1 = Entry(parent) 
     #create button 
     self.button1 = Button(parent, text='Button1', command=self.hello) 

     #string variable to hold checkbox1 values. 
     self.str_checkbox1 = StringVar()   
     #create checkbox 
     self.checkbox1 = Checkbutton(parent, text='Checkbox1', variable=self.str_checkbox1, onvalue='on1', offvalue='off1') 
     #deselect checkbox1 
     self.checkbox1.deselect() 
     #string variable to hold checkbox2 values. 
     self.str_checkbox2 = StringVar()  
     #create checkbox 
     self.checkbox2 = Checkbutton(parent, text='Checkbox2', variable=self.str_checkbox2, onvalue='on2', offvalue='off2') 
     #deselect checkbox2 
     self.checkbox2.deselect() 

     #???? ..what sets the groupbox apart from others. primary key???!! 
     self.str_radiobutton1 = StringVar() 
     #command= parameter missing. 
     self.radiobutton1 = Radiobutton(parent, text='Radio 1', variable=self.str_radiobutton1, value='a') 
     self.radiobutton2 = Radiobutton(parent, text='Radio 2', variable=self.str_radiobutton1, value='b') 
     self.radiobutton1.select() 

     #create a list of options. 
     optionList = ('Option1', 'Option2', 'Option3') 
     #string variable to hold optionlist values. 
     self.str_optionmenu1 = StringVar() 
     #associate string variable with optionlist 
     self.str_optionmenu1.set(optionList[0]) 
     #create optionmenu 
     self.optionmenu1 = OptionMenu(parent, self.str_optionmenu1, *optionList) 

     #create a frame 
     self.frame1 = Frame(parent) 
     #create a text. 
     self.textarea1 = Text(self.frame1, width=20, height=10) 
     #align text left and fill frame with it. 
     self.textarea1.pack(side=LEFT, fill=Y) 
     #create a scrollbar. 
     self.scrollbar1 = Scrollbar(self.frame1) 
     #align scrollbar right and fill frame with it. 
     self.scrollbar1.pack(side=RIGHT, fill=Y) 
     #what is going to be scrolled? 
     self.scrollbar1.config(command=self.textarea1.yview) 
     #set textarea scrollbar. 
     self.textarea1.config(yscrollcommand=self.scrollbar1.set) 
     #align frame left and fill. 
     self.frame1.pack(side=LEFT, fill=Y) 

     #create a frame 
     self.frame2 = Frame(parent) 
     #create a text. 
     self.listbox1 = Listbox(self.frame2, width=20, height=10, activestyle='none', selectmode=SINGLE) 
     #create a list of items. 
     optionList = ('Item1', 'Item2', 'Item3', 'Item4', 'Item5', 'Item6', 'Item7', 'Item8', 'Item9', 'Item10', 'Item11') 
     #add items from list to listbox 
     for item in optionList: 
      self.listbox1.insert(END, item) 
     #align text left and fill frame with it. 
     self.listbox1.pack(side=LEFT, fill=Y) 
     #create a scrollbar. 
     self.scrollbar2 = Scrollbar(self.frame2) 
     #align scrollbar right and fill frame with it. 
     self.scrollbar2.pack(side=RIGHT, fill=Y) 
     #what is going to be scrolled? 
     self.scrollbar2.config(command=self.listbox1.yview) 
     #set textarea scrollbar. 
     self.listbox1.config(yscrollcommand=self.scrollbar2.set) 
     #align frame left and fill. 
     self.frame2.pack(side=LEFT, fill=Y) 


     # Place controls inside of grid 
     self.label1.grid(row=0, column=0, padx=pad_x, pady=pad_y, sticky=W) 
     self.textbox1.grid(row=0, column=1, padx=pad_x, pady=pad_y, sticky=W) 
     self.button1.grid(row=1, column=0, padx=pad_x, pady=pad_y, sticky=W) 
     self.checkbox1.grid(row=1, column=1, padx=pad_x, pady=pad_y, sticky=W) 
     self.checkbox2.grid(row=1, column=2, padx=pad_x, pady=pad_y, sticky=W) 
     self.optionmenu1.grid(row=2, column=0, padx=pad_x, pady=pad_y, sticky=W) 
     self.frame1.grid(row=2, column=1, padx=pad_x, pady=pad_y, sticky=W)   
     self.radiobutton1.grid(row=3, column=0, padx=pad_x, pady=pad_y, sticky=W) 
     self.radiobutton2.grid(row=3, column=1, padx=pad_x, pady=pad_y, sticky=W) 
     self.frame2.grid(row=4, column=0, padx=pad_x, pady=pad_y, sticky=W) 

if __name__ == '__main__': 
    parent = Tk() 
    app = Application(parent) 
    parent.mainloop() 

回答

0

好的。顯然,tkMessageBox已經在Python 3.x中重命名爲messagebox。另外這款 模塊不在Tkinter的可用,因此即使開發者可以使用:

from tkinter import * 

..he /她仍然需要:

from tkinter import messagebox