代码之家  ›  专栏  ›  技术社区  ›  wayoh22

Tkinter使用grid_configure调整标签大小

  •  0
  • wayoh22  · 技术社区  · 6 年前

    我试图让我的标签、输入字段和按钮在窗口大小改变时自动调整大小。我已经知道了如何拥有一个相应调整大小的背景图像,但不知道如何使用它 grid_columnconfigure() 和 grid_rowconfigure() 做得恰当。我曾尝试使用pack()将所有内容按照我的意愿安排好,但未能实现。我需要把它保持在3列。还有一种方法可以使标题平均居中吗?我试过了 columnspan 和 rowspan 但那里也没有骰子。如果运行此命令,则需要在“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", foreground = "dark green", font = ("Times New Roman", 20), borderwidth = 2, relief = "solid")
    question_1.grid(column = 0, row = 1)
    question_2 = Label(window, text = "Insert Question Here?", background = "gray23", foreground = "dark green", font = ("Times New Roman", 20), borderwidth = 2, 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()
    

    谢谢

    0 回复  |  直到 6 年前