有没有一种方法可以使Button执行多个命令

时间:2019-08-24 07:59:01

标签: python tkinter

我正在做一个学校项目。我在tkinter上为此设计了一个欢迎页面,并放置了一个“确定”按钮,按下该按钮可使代码向前移动,但是一旦被按下,欢迎页面不会自动关闭。

我尝试定义另一个函数来关闭它,但这不起作用。

welcome = Tk()
okbutton = Button(welcome, text='ok', command=R)
okbutton.pack()
welcome.mainloop()

代码继续前进,但欢迎页面仍处于打开状态...是否有解决此问题的方法?

3 个答案:

答案 0 :(得分:0)

要执行这两个命令,请在另一个命令中调用一个命令(听起来应该像结尾一样),然后将第一个命令分配给按钮。

答案 1 :(得分:0)

创建新窗口时,窗口永远不会自动关闭。您必须为此使用welcome.destroy()。您可以在创建新窗口的函数中运行它。

import tkinter as tk


def welcome_page():
    global welcome

    welcome = tk.Tk()

    tk.Label(welcome, text='Welcome').pack()

    button = tk.Button(welcome, text='OK', command=other_page)
    button.pack()

    welcome.mainloop()


def other_page():
    global welcome
    global other

    welcome.destroy() # close previous window

    other = tk.Tk()

    tk.Label(other, text='Other').pack()

    button = tk.Button(other, text='OK', command=end)
    button.pack()

    welcome.mainloop()


def end():
    global other

    other.destroy() # close previous window


welcome_page()    

答案 2 :(得分:0)

按钮只能调用一个功能,但是该功能可以执行您想要的任何操作。

def do_ok():
    print("hello!")
    welcome.destroy()

welcome = Tk()
okbutton = Button(welcome, text='ok', command=do_ok)
okbutton.pack()
welcome.mainloop()