【问题标题】:In Python, how do I assign a Tkinter button to a piece of code?在 Python 中,如何将 Tkinter 按钮分配给一段代码?
【发布时间】:2015-05-11 16:53:42
【问题描述】:

我无法为此编写代码,我想知道你们中是否有人这样做。我想将 tkinter 菜单中的按钮分配给另一段 Python 代码中定义的函数(或者有某种方式链接它们)。我尝试了许多不同的网站,但我无法轻易找到。

我尝试过的网站:

-https://docs.python.org/3/library/tkinter.ttk.html

-www.tutorialspoint.com/python/tk_button.htm

【问题讨论】:

  • 你能发布你的代码吗?
  • 需要查看您的代码,有很多方法可以做到吗?

标签: python tkinter


【解决方案1】:

This 是我在 google 中得到的第一个结果:

from Tkinter import *

master = Tk()

def callback():
    print "click!"

b = Button(master, text="OK", command=callback)
b.pack()

mainloop()

如您所见,command 用于分配回调函数,以便在用户单击按钮时运行。

【讨论】:

  • 非常感谢。这对我帮助很大。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-30
  • 2012-10-21
相关资源
最近更新 更多