tkinter按钮的多个命令 [英] multiple commands for a tkinter button

查看:85
本文介绍了tkinter按钮的多个命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图在1个按钮中执行2个命令.我已经读过使用lambda可以解决问题.但是我的情况有些不同.按下按钮时,一个命令是销毁现有的GUI,第二个命令是要打开另一个SERVER GUI.以下是我现有的按钮功能.

I have been trying to execute 2 commands in 1 button. I have read that using lambda can solve the problem. But my situation is a little different. On button press, one command is to destroy the existing GUI, and the second command, I want is to open another SERVER GUI. Below is my existing button functionality.

exit_button = Button(topFrame, text='Quit', command=window.destroy)

如何使用相同的按钮打开另一个GUI?

How can I open another GUI using same button?

谢谢您的帮助.

-

我现在创建了以下功能:

I have now created the below function:

def close_func():
os.kill(os.getpid(), signal.SIGINT)
GUI_Interface()
window.destroy()
server_socket.close()

GUI_Interface是关闭现有.py文件后需要调用的函数.如果我将GUI_Interface作为close_func()的第一个命令,那么它实际上不会返回到第二步,也永远不会关闭existing.py文件.

GUI_Interface is a function that I need to call after closing the existing .py file. If I put GUI_Interface as the first command for close_func(), then it really does not go back to the 2nd step and never closes the existing.py file.

如果我将GUI_Interface放在最后,它只会关闭现有的一个,而nevr会打开新.py文件的功能

And if I place GUI_Interface in the end, it just closes the existing one and nevr opens the function of the new .py file

推荐答案

至少三个选项:

使用或(如果有参数,则使用lambda):

using or (with lambda if arguments):

from tkinter import Tk, Button


root = Tk()

Button(root, text='Press', command=lambda: print('hello') or root.destroy() or print('hi')).pack()

root.mainloop()

使用或很重要,因为不起作用(根本不知道为什么或如何起作用)

important to use or because and didn't work (don't know why or how this works at all)

或使用函数定义:

from tkinter import Tk, Button


def func():
    print('hello')
    root.destroy()
    print('hi')


root = Tk()

Button(root, text='Press', command=func).pack()

root.mainloop()

或带有lambda的列表:

or lists with lambda:

from tkinter import Tk, Button


def func():
    print('hello')
    root.destroy()
    print('hi')


root = Tk()

Button(root, text='Press', command=lambda: [print('hello'), root.destroy()]).pack()

root.mainloop()

这篇关于tkinter按钮的多个命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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