【问题标题】:python tkinter Can I make a widget in pixel? with text?python tkinter 我可以用像素制作一个小部件吗?与文字?
【发布时间】:2020-10-29 12:05:00
【问题描述】:

我想制作一个像素大小的文本小部件。但始终设置字体大小。

这是我的代码:

 import tkinter as tk
 root = tk.Tk()
 test_btn = tk.Button(root, text="test", height=10, width=10)
 test_btn.pack()
 root.mainloop()

我想使用像素,但我不能这样做,我使用的是“放置”方法,所以我的代码看起来很脏。 我想将它们更改为“像素大小”并打包。

【问题讨论】:

  • 为其分配一个像素图像并设置compound='center'
  • 谢谢!!我可以再问一个问题吗?我想让 root_background 模糊。 window.wait_visibility(window) window.wm_attributes('-alpha',0.3)
  • '-alpha' 应该在 Linux 下的 tk 8.6 中工作。

标签: python tkinter


【解决方案1】:

如果按钮有文本,width 属性以字符为单位,但如果按钮有图像,则以像素为单位。因此,您可以向按钮添加一个小的透明图像,并设置compound 选项以同时允许文本和图像。当您这样做时,width 将被解释为像素。

例子:

image = tk.PhotoImage(width=1, height=1)
button = tk.Button(root, image=image, text="Hello, world", compound="center",width=300)

【讨论】:

    猜你喜欢
    • 2021-11-11
    • 1970-01-01
    • 2017-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多