Python TKinter GUI [英] Python TKinter GUI

查看:26
本文介绍了Python TKinter GUI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个关于电子学习的申请.在这个特定的页面上,我希望能够为用户制作按钮以从简单、中等和困难中选择难度级别.按下按钮后,用户将进入下一个页面,可以是 EasyLevel、ModerateLevel 或 HardLevel.如何修复我的代码?

Im making an application about e-learning. On this particular page i want to be able to make buttons for the user to choose the difficulty level from easy, moderate and hard. After the button is pressed, the user will proceed to the next page either EasyLevel, ModerateLevel or HardLevel. How to fix my codes?

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代码然后点击按钮就可以打开另一个窗口并关闭最后一个.

Actually you need follow oops concept in your code , just make a class and then in its initialization , write your first UI code then on button click you can open anther window and close last one.

像这样写的代码:-

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

这篇关于Python TKinter GUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆