Tkinter使用grid_configure调整标签大小

问题描述

如果窗口大小已更改,我正在尝试自动调整标签,输入字段和按钮的大小。我已经想出了如何具有可以相应调整大小的背景图像,但是无法弄清楚如何使用grid_columnconfigure()grid_rowconfigure()来适当地进行操作。我试过使用pack()将所有内容按我的意愿进行排列,但无法获取。我需要将其总体上保持3列。还有没有办法使标题均匀居中?我尝试过columnspanrowspan,但也没有骰子。如果运行此代码,则需要将图像插入“ image”变量中。我正在使用的代码是:

from tkinter import ttk
from PIL import Image,ImageTk

window = Tk()
window.title("Insert Window Title Here")
window.geometry('800x400')

def resize_image(event):
    new_width = event.width
    new_height = event.height
    image = copy_of_image.resize((new_width,new_height))
    photo = ImageTk.PhotoImage(image)
    label.config(image = photo)
    label.image = photo #avoid garbage collection

image = Image.open("/Users/useradmin/Tkinter_Projects/Untitled-1.png")
copy_of_image = image.copy()
photo = ImageTk.PhotoImage(image)
label = ttk.Label(window,image = photo)
label.bind('<Configure>',resize_image)
label.place(x = 0,y = 0,relwidth = 1,relheight = 1)

title = Label(window,text = "Insert Title Here...",background = "black",foreground = "dark green",font = ("Times New Roman",32),borderwidth = 2,relief = "solid")
title.grid(column = 1,row = 0)

question_1 = Label(window,text = "Insert Question Here?",background = "gray23",20),relief = "solid")
question_1.grid(column = 0,row = 1)
question_2 = Label(window,relief = "solid")
question_2.grid(column = 0,row = 2)

entry_1 = Entry()
entry_1.grid(column = 1,row = 1)
entry_2 = Entry()
entry_2.grid(column = 1,row = 2)

question_1_button = Button(window,text = "Insert Something Here")
question_1_button.grid(column = 2,row = 1)
question_2_button = Button(window,text = "Insert Something Here")
question_2_button.grid(column = 2,row = 2)
question_3_button = Button(window,text = "Insert Something Here")

window.mainloop()

谢谢。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)