繁体   English   中英

Python TKinter GUI

[英]Python TKinter GUI

我正在制作有关电子学习的应用程序。 在此特定页面上,我希望能够为用户提供按钮,以从简单,中等和困难中选择难度级别。 按下按钮后,用户将进入下一页EasyLevel,ModerateLevel或HardLevel。 如何修正我的密码?

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

实际上,您需要在代码中遵循oops概念,只需创建一个类,然后在其初始化中编写您的第一个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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM