如何在python中制作图形按钮?

如何在python中制作图形按钮?,python,Python,我想在我的命令行游戏中添加一些gui按钮。单击一个按钮,我希望执行我的一个函数。我该怎么办?谢谢 对于命令行游戏,您不应该包含GUI(因此是命令行)。但是,如果你真的想要一个,你可以这样做: from Tkinter import * import thread root = Tk() root.title("My Game") def someFunct(): # add whatever you want to do pass myButton = Button(root,

我想在我的命令行游戏中添加一些gui按钮。单击一个按钮,我希望执行我的一个函数。我该怎么办?谢谢

对于命令行游戏,您不应该包含GUI(因此是命令行)。但是,如果你真的想要一个,你可以这样做:

from Tkinter import *
import thread

root = Tk()
root.title("My Game")
def someFunct():
    # add whatever you want to do
    pass
myButton = Button(root, text = "My Button", command = someFunct)
# if you want to use arguments in the command, do this:
# command = lambda: someFunct(arg1, arg2, etc.)
myButton.grid()

def main():
    # main game, add your command line stuff here
    pass

thread.start_new_thread(main, ()) # runs the main program
root.mainloop() # also runs the GUI

在线程中运行GUI似乎更符合逻辑(
thread.start\u new\u thread(root,())
),但我认为Tkinter本身使用线程,因此这会导致一些问题(不确定Tkinter是否使用线程,但
thread.start\u new\u thread(root,())
在我的一些程序中已冻结).

您建议如何在命令行游戏中添加按钮的可能重复?这是命令行,因为您没有完整的游戏GUI。感谢您提供的信息!我只是想将我的命令行游戏切换到一个真正的GUI,但我知道这需要相当多的工作。