【问题标题】:How to use activeforeground option in button widget of tkinter python如何在 tkinter python 的按钮小部件中使用 activeforeground 选项
【发布时间】:2021-05-28 13:42:32
【问题描述】:

我是 Tkinter 的新手,我的问题很简单,在按钮小部件中有 activeforeground 选项,我想知道如何使用它。

【问题讨论】:

  • 我认为 activeforeground 是按下按钮时按钮内文本的颜色 - 按住左键单击。
  • 你能发布你的代码吗?

标签: python tkinter button


【解决方案1】:

您在 tkinter 中指定一个按钮,如下所示:Button(<parent>, text=<text>, activeforeground=<colour>,....) 由于 activeforeground 是一个关键字参数,您只需将它放在括号中并给它一个颜色。或者你到底想做什么?

这是一个带有按钮的简单窗口,您可以在这里看到activeforeground是如何实现的以及点击按钮时会发生什么。

import tkinter
from tkinter import*

root = tkinter.Tk()

Button1 = tkinter.Button(root,text="click me!",activeforeground="red") #<---- HERE
Button1.place(relx=0.5,rely=0.5,anchor=CENTER)

root.mainloop()

此页面提供了不同关键字的列表: https://www.tutorialspoint.com/python/tk_button.htm

【讨论】:

  • 首先这些是关键字参数而不是方法。其次,你写的这件事:Button("your window", "method1","method2"....methodn") 很容易被错误地解释。我会使用:Button(&lt;parent&gt;, text=&lt;text&gt;, activeforeground=&lt;colour&gt;),然后指定颜色和文本是字符串。
  • 你的描述更好,但是的,这就是我想要告诉的内容
  • 也不是很重要,但如果您要展示一个最小的示例,请使用以下内容:root = tkinter.Tk()button = tkinter.Button(root, text="Click me", activeforeground="red")button.pack()root.mainloop()。这将整个代码简化为 5 行,并且仅使用 2 个变量。您的答案越简单,对 OP 的帮助就越大
  • 可能并不重要,但它使它干净直接,感谢您的帮助。我希望他现在能够理解这一点。
  • 非常感谢您在我查看教程点网站之前回答了我的问题,但是当我实现它时,我没有使用按钮中的文本,所以这是造成混乱的原因
猜你喜欢
  • 2022-06-18
  • 2013-10-22
  • 2018-09-13
  • 1970-01-01
  • 1970-01-01
  • 2020-11-17
  • 2019-01-04
  • 2022-01-20
  • 2021-07-31
相关资源
最近更新 更多