2016-02-14 119 views
0

我正在制作一个关于电子学习的应用程序。在这个特定的页面上,我希望能够让用户从简单,中等和难度中选择难度级别的按钮。按下按钮后,用户将进入下一页EasyLevel,ModerateLevel或HardLevel。如何解决我的代码?Python TKinter GUI

import Tkinter 
    LevelBox = Tkinter.Tk() 
    LevelBox.geometry("320x260") 
    LevelBox.title("Diffuculty") 
    LevelBox.withdraw() 

    def Easy() : 
    LevelBox.withdraw() 
    easybox.deiconify() 
    return 

    def Moderate() : 
    LevelBox.withdraw() 
    moderatebox.deiconify() 
    return 

    def Hard() : 
    LevelBox.withdraw() 
    hardbox.deiconify() 
    return 

    b1 = Tkinter.Button (LevelBox, text="Easy",   command=Easy,height=1,width=7).grid(row=1,column=1,sticky="e",pady=5,padx=5) 
    b1 = Tkinter.Button (LevelBox, text="Moderate", command=Moderate,height=1,width=7).grid(row=1,column=3,sticky="w",pady=5,padx=5) 
    b1 = Tkinter.Button (LevelBox, text="Hard", command=Hard,height=1,width=7).grid(row=2,column=1,sticky="e",pady=5,padx=5) 

    easybox = Tkinter.Toplevel() 
    easybox.geometry("320x260") 
    easybox.title("Easy Questions") 
    easybox.withdraw() 

    moderatebox = Tkinter.Toplevel() 
    moderatebox.geometry("320x260") 
    moderatebox.title("Moderate Questions") 
    moderatebox.withdraw() 

    hardbox = Tkinter.Toplevel() 
    hardbox.geometry("320x260") 
    hardbox.title("Hard Questions") 
    hardbox.withdraw() 

回答

0

其实你需要遵循哎呀概念在你的代码,只是做一个类,然后在它的初始化,写你的第一个UI代码然后点击链接就可以打开花药窗口关闭最后一个。

这样的代码写的是: -

import Tkinter as tk 

class MainWindow(tk.Frame): 
    counter = 0 
    def __init__(self, *args, **kwargs): 
     tk.Frame.__init__(self, *args, **kwargs) 
     self.button = tk.Button(self, text="Create new window", 
           command=self.create_window) 
     self.button.pack(side="top") 

    def create_window(self): 
     self.counter += 1 
     t = tk.Toplevel(self) 
     t.wm_title("Window #%s" % self.counter) 
     l = tk.Label(t, text="This is window #%s" % self.counter) 
     l.pack(side="top", fill="both", expand=False, padx=100, pady=100) 

if __name__ == "__main__": 
    root = tk.Tk() 
    main = MainWindow(root) 
    main.pack(side="top", fill="both", expand=False) 
    root.geometry('300x200') 
    root.attributes("-toolwindow", 1) 
    root.mainloop() 
+1

__tkinter.TclError:坏属性 “-toolwindow”:必须是-α,-topmost,-zoomed,-fullscreen,或-type_ – zondo

+0

我米使用python 2.7.11,可能是因为版本而发生的。所以我必须在python 3.5中导入Tkinter,它应该是这样的** import tkinter as tk ** ..你使用的是什么版本的python? –

+0

我在Python2.7.6和Python3.4.3中试过它,并且在两者中都得到相同的错误。 – zondo