首页 > 解决方案 > 如何阻止 tkinter 标签变小?

问题描述

我正在构建一个简单的 python 程序来使用 tkinter 创建一个窗口,其背景存储为计算机上的图像。

这是我的代码:

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() 来阻止标签缩小,但它不起作用。

有谁知道如何阻止图片缩小?

谢谢

标签: pythontkintertkinter-canvastkinter-label

解决方案


我认为这会奏效

bgclass=bgclass.resize((700,700), Image.ANTIALIAS)
background = Label(canvas, image=bgClass)
background.pack()

在这里,我已将图像大小调整为 700x700,您可以根据需要进行更改。


推荐阅读