Tkinter(python 2.7.2)需要帮助

时间:2011-10-22 21:07:06

标签: python tkinter python-2.7

我正在尝试制作我的第一个GUI程序。问题是,我无法弄清楚如何制作一个主菜单,它会在点击一个按钮后切换到其中一个程序。

#Dev by Mkee
from Tkinter import *
import tkMessageBox
#Main Stuff
app = Tk()
app.title("Mkee's Tools")
app.geometry('300x200')

#modules
class Programs:
    def Shuffle():
        app2 = Tk()
        app2.title("Shuffle")
        app2.geometry('300x200')
        app2.mainloop()


#end of modules

labelText = StringVar()
labelText.set('')
label1 = Label(app, textvariable=labelText, height=4)
label1.pack()

button1 = Button(app, text='Shuffle', width=30, command=Programs.Shuffle)
button1.pack(side='right', padx=5,pady=1)


app.mainloop()

我知道我做错了。我只是不知道该怎么做,所以我尝试了它怎么可能。请帮帮我。

1 个答案:

答案 0 :(得分:1)

  1. 您可以调用pack_forget()隐藏小部件,然后(稍后) pack再次向他们展示:

  2. Tkinter是单线程的,mainloop运行主事件循环。因此,您不应该拨打mainloop两次。


  3. #Dev by Mkee
    import Tkinter as tk
    import sys
    
    class Shuffle(object):
        def __init__(self,master=None):
            self.master=master
            self.text=tk.Text(master)
        def hide(self):
            self.text.pack_forget()
        def show(self):
            self.text.pack(side=tk.LEFT, padx=5, pady=5)
    
    class Buttons(object):
        def __init__(self,master=None):
            self.master=master
            self.red = tk.Button(self.master, text="Red", bg="red", fg="white")
            self.green = tk.Button(self.master, text="Green", bg="green", fg="black")
            self.blue = tk.Button(self.master, text="Blue", bg="blue", fg="white")
        def hide(self):
            self.red.pack_forget()
            self.green.pack_forget()
            self.blue.pack_forget()                
        def show(self):
            self.red.pack(side=tk.LEFT,expand=tk.YES,fill=tk.BOTH)
            self.green.pack(side=tk.LEFT,expand=tk.YES,fill=tk.BOTH)
            self.blue.pack(side=tk.LEFT,expand=tk.YES,fill=tk.BOTH)
    
    class MainApp(object):
        def __init__(self,master=None):
            self.master=master
            app=self.app=tk.Tk()
            app.title("Mkee's Tools")
            app.geometry('300x200')
            self.shuffle=Shuffle(master)
            self.buttons=Buttons(master)
            self.current=None
            menubar=tk.Menu(app)
            program_menu=tk.Menu(menubar)
            program_menu.add_command(label='Shuffle',
                                     command=lambda: self.show(self.shuffle))
            program_menu.add_command(label='Buttons',
                                     command=lambda: self.show(self.buttons)) 
            program_menu.add_command(label='Quit',command=sys.exit) 
            menubar.add_cascade(label='Programs', menu=program_menu)
            app.config(menu=menubar)
        def show(self,obj):
            if self.current != obj:
                try: self.current.hide()
                except AttributeError: pass
                self.current=obj
                obj.show()
    
    def main():
        m=MainApp()
        m.app.mainloop()
    
    if __name__=='__main__':
        main()