首页 > 解决方案 > Tkinter 网格不工作

问题描述

我想创建一个窗口,内容都在中心,但我不知道该怎么做,请帮助我。

def about_window():
    win_about = tk.Toplevel(win)
    win_about.geometry("340x500")
    win_about.title("About Us")
    win_about.resizable(0,0)
    win_about.iconbitmap(r'C:/Users/810810/Desktop/python/eslogo.ico')
    frame = tk.Frame(win_about)
    frame.grid(row=0, column=2)

    img_png = tk.PhotoImage(file = 'est.gif')
    label = tk.Label(frame, image = img_png)
    label.img_png = img_png
    label.grid(row=0, column=1)

    Message = 'Version: 1.0'
    mess = tk.Label(frame, text=Message)
    mess.grid(row=1, column=0)

标签: pythontkinter

解决方案


我也有很多问题tkinter grid并且更喜欢使用tkinter place.

下面我编辑了您的代码以使用place而不是grid. anchor指的是您正在移动的对象的锚点,relx指的是相对 x 位置,以它所在的帧的百分比表示(.5意思是在帧的中间),并rely指从 0-1 的帧中的 y 位置。

import tkinter as tk

win_about = tk.Tk()
win_about.geometry("340x500")
win_about.title("About Us")
win_about.resizable(0,0)

label = tk.Label(win_about, text="img_png", fg="black")
label.place(anchor='center', relx =.5, rely=.3)

mess = tk.Label(win_about, text='Version: 1.0', font=12)
mess.place(anchor='center', relx=.5, rely=.7)

win_about.mainloop()

推荐阅读