python - 如何阻止 tkinter 标签变小?
问题描述
我正在构建一个简单的 python 程序来使用 tkinter 创建一个窗口,其背景存储为计算机上的图像。
我首先制作了一个全尺寸的窗口并显示它。
我在这个窗户上放了一张画布
然后我使用了 photoimage 类并使用了我的 png 图像。然后我把它贴在标签上。
此时,当我运行代码时,它运行良好。
我试图在标签顶部添加一个按钮,现在发生的情况是带有图片的标签缩小了,按钮周围几乎看不到图片。
这是我的代码:
from tkinter import *
import pyautogui as pag
def act():
print("Hey")
##action here
root = Tk()
winWidth, winHeight = pag.size()
root.state("zoomed")
canvas = Canvas(root, width=winWidth, height=winHeight, bg="white")
canvas.pack()
canvas.pack_propagate(0)
bgClass = PhotoImage(master=root, file="backg.png")
background = Label(canvas, image=bgClass, width=winWidth, height=winHeight)
background.pack()
b = Button(background, text="Click me", command=act)
b.pack()
root.mainloop()
我尝试使用 pack_propagate() 来阻止标签缩小,但它不起作用。
有谁知道如何阻止图片缩小?
谢谢
解决方案
我认为这会奏效
bgclass=bgclass.resize((700,700), Image.ANTIALIAS)
background = Label(canvas, image=bgClass)
background.pack()
在这里,我已将图像大小调整为 700x700,您可以根据需要进行更改。
推荐阅读
- excel - Set variable to a sheet's Codename, Using it in the code isn't working
- mysql - 获取列中具有连续数字的唯一 ID 列表
- android - content_main.xml 有问题
- android - React Native、Native Android Module在View中添加TextView
- python - 尝试根据用户输入从列表列表中返回相关信息
- python - 字典熊猫中的展平列表
- java - 从异常处理程序获取元素未找到异常
- mysql - 如何从 SQL Server 生成 XML 日志文件
- python - pip 包在 OSX 中创建可执行文件,但在 Ubuntu 中没有
- python - 根据条件加入有或没有最后一个元素的列表的pythonic方法