如何在调整窗口大小时调整 tkinter 小部件的大小? [英] How do I resize tkinter widgets as window is resized?

查看:122
本文介绍了如何在调整窗口大小时调整 tkinter 小部件的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Python 新手,我正在使用 Python 为事件创建倒数计时器,我注意到画布中的图像在较大的显示器上没有填满整个屏幕,并且小部件在窗户缩小了.我该如何解决?任何帮助表示赞赏.谢谢!

I'm new to python and I'm creating a countdown timer for an event with Python and I noticed that the image within the canvas doesn't fill up the entire screen on larger display's and the widgets don't resize when the window is shrunk. How do I fix this? Any help is appreciated. Thanks!

import datetime
import tkinter as tk
import winsound

def round_time(dt, round_to):
    seconds = (dt - dt.min).seconds
    rounding = (seconds + round_to / 2) // round_to * round_to
    return dt + datetime.timedelta(0, rounding - seconds, -dt.microsecond)


def ct():
    def count():
        now = round_time(datetime.datetime.now(), round_to=1)
        eh = datetime.datetime(2019, 3, 30, 20, 30)
        tte = eh - now
        canvas.itemconfig(label_cd, text=str(tte))
        root.after(50, count)

    count()


root = tk.Tk()

winsound.PlaySound('Sound1.wav', winsound.SND_ASYNC + winsound.SND_LOOP)
root.bind('<space>', lambda a: winsound.PlaySound(None, winsound.SND_PURGE))
root.bind('<s>', lambda a: winsound.PlaySound('Sound1.wav', winsound.SND_ASYNC + winsound.SND_LOOP))

root.geometry('1920x1080')
root.attributes('-fullscreen', True)
root.bind('<Escape>', lambda e: root.attributes("-fullscreen", False))
root.bind('<F11>', lambda z: root.attributes("-fullscreen", True))
root.title("Earth Hour Countdown!")
now = round_time(datetime.datetime.now(), round_to=1)
eh = datetime.datetime(2019, 3, 30, 20, 30)
tte = eh - now

canvas = tk.Canvas(root, height=1000, width=600, highlightthickness=0)
canvas.place(relheight=1, relwidth=1)

bg_img = tk.PhotoImage(file="new.gif")
bg_label = canvas.create_image((682.5, 384), image=bg_img)

cte_logo = tk.PhotoImage(file="cte1.gif")
cte_label = canvas.create_image((690, 120), image=cte_logo)

logo = tk.PhotoImage(file="eh60.gif")
logo_canvas = canvas.create_image((715, 590), image=logo)

label_msg = canvas.create_text((410, 300), text="Earth Hour Countdown:", font="Calibri 60 bold", fill="#CDCEDF")

label_cd = canvas.create_text((1080, 300), text=str(tte), font="Calibri 60 bold", fill="#CDCEDF")

ehtime_label = canvas.create_text((700, 400), text=("Earth Hour: " + eh.strftime("%d-%m-%Y %H:%M:%S")), font="Calibri 60 bold", fill="#CDCEDF")

ct()

root.mainloop()

  • 预期结果:https://imgur.com/LkrbJa0
  • 放大显示的结果:https://imgur.com/MtAYSIs
  • 窗口缩小时的结果:https://imgur.com/bAjst6L
  • 推荐答案

    有关在 Python 中调整图像大小的信息,请查看 Python 图像库 (PIL).有几个关于 PIL 使用的现有帖子,它们帮助我编写了下面的代码.使用从运行平台获取的屏幕尺寸和正确"尺寸图像的参考尺寸,很容易计算调整尺寸因子.然后可以通过将此因子应用于参考屏幕尺寸(在本例中为 270x185)的图像尺寸来获得扩展/收缩显示所需的尺寸.代码的其余部分只是扫描参考图像的文件夹 (Assets_dir),打开它们并使用 PIL.Image 调整大小,然后将调整大小的图像保存到新文件夹中.

    For image re-sizing in Python, check out the Python Image Library (PIL). There are several existing posts on the use of PIL, which helped me to write the code below. With the screen dimensions acquired from the running platform and reference dimensions for "correctly" sized images, it is easy to calculate a re-sizing factor. Then the dimensions needed for the expanded/shrunk display can be obtained by applying this factor to the dimensions of the image at the reference screen size (in this case 270x185). The remainder of the code just scans a folder of the reference images (Assets_dir), opens them and resizes with PIL.Image and then saves the resized image to a new folder.

    import PIL.Image
    
    screen_width = root.winfo_screenwidth()
    screen_height = root.winfo_screenheight()
    width = 1920.0                                #  default screen width/height stored images are based on.
    height = 1200.0
    width_resize = screen_width/width             #  calculate resize factor needed to correctly display on current screen.
    height_resize = screen_height/height
    
    new_width = int(width_resize*270)
    new_height = int(height_resize*185)
    
    Assets_dir = os.getcwd() + '\\Assets\\'
    files = list(os.scandir(Assets_dir))
    for file in files:
        file_ext = file.name[-3:]
        file_basename = file.name[:-4]
        if file_ext == 'gif':
            temp_image = PIL.Image.open(Assets_dir + file.name)
            temp_image = temp_image.resize((new_width, new_height), PIL.Image.ANTIALIAS)
            temp_image.save(Assets_dir + r'/resize/' + file.name)
    

    这篇关于如何在调整窗口大小时调整 tkinter 小部件的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆