【问题标题】:Tkinter with Python [duplicate]Tkinter 与 Python [重复]
【发布时间】:2017-05-15 21:00:02
【问题描述】:

我对 Python 还是很陌生,昨天开始学习 Tkinter。我正在创建一个银行系统,我有一个由按钮组成的菜单。我遇到的问题是单击按钮时我不知道如何打开其他窗口。我试过top=Toplevel(),但它只打开了两个相互重叠的窗口。我需要的是仅在使用按钮(事件)激活附加窗口时才打开它。有人可以帮我吗,因为我有大约六个按钮,我真的被卡住了?

到目前为止我的代码是:

root.minsize(width=400, height=400)
root.maxsize(width=400, height=400)
root.configure(background='#666666')
label = Frame(root).pack()
Lb = Label(label, text='Welcome to Main Menu',bg='#e6e6e6', width=400).pack()

menu = Frame(root,).pack()
btn_1 = Button(menu, text='Make Deposit', width=15, height=2).pack(pady=5)
btn_2 = Button(menu, text='Withdrawal', width=15, height=2).pack(pady=5)
btn_3 = Button(menu, text='Accounts', width=15, height=2).pack(pady=5)
btn_4 = Button(menu, text='Balance', width=15 ,height=2).pack(pady=5)
btn_5 = Button(menu, text='Exit', width=15, height=2).pack(pady=5)
root.mainloop()

提前感谢您的帮助

【问题讨论】:

  • 您需要将打开Toplevel的函数传递给按钮的command选项。

标签: python tkinter


【解决方案1】:

也许这可行:

from Tkinter import *

class firstwindow:
    def __init__(self):
        #Variables
        self.root= Tk()
        self.switchbool=False
        #Widgets
        self.b = Button(self.root,text="goto second window",command= self.switch)
        self.b.grid()
        self.b2 = Button(self.root,text="close",command= self.root.quit)
        self.b2.grid()
        self.root.mainloop()

    #Function to chage window
    def switch(self): 
        self.switchbool=True
        self.root.quit()
        self.root.destroy()

class secondwindow:
    def __init__(self):
        self.root= Tk()
        self.b2 = Button(self.root,text="close",command= self.root.quit)
        self.b2.grid()
        self.root.mainloop()

one = firstwindow()

if one.switchbool:
    two = secondwindow()

看看Tkinter reference。检查所有通用方法和小部件方法。你可以做任何你想做的事。

【讨论】:

    【解决方案2】:

    下面是一个小示例,说明如何通过单击按钮打开窗口。当用户点击按钮时,会调用传递给按钮command选项的函数open_window

    import tkinter as tk
    
    def open_window():
        top = tk.Toplevel(root)
        tk.Button(top, text='Quit', command=top.destroy).pack(side='bottom')
        top.geometry('200x200')
    
    root = tk.Tk()
    
    tk.Button(root, text='Open', command=open_window).pack()
    tk.Button(root, text='Quit', command=root.destroy).pack()
    
    root.mainloop()
    

    【讨论】:

      猜你喜欢
      • 2021-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-26
      • 2020-07-29
      • 2014-01-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多