python - Tkinter 网格不工作

标签 python 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)

最佳答案

我在使用 tkinter grid 时也遇到了很多问题,并且更喜欢使用 tkinter place

下面我编辑了您的代码以使用 place 而不是 gridanchor 是指您正在移动的对象的 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()

关于python - Tkinter 网格不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51453576/

相关文章:

python - 重新采样表示图像的 numpy 数组

python - numpy 多维数组索引

python - Tkinter 窗口格式化 (Python)

python - 用于级别名称的 Pandas 多索引切片

python - 在 Plotly 中创建 3D 曲面图

python - 来自 sk_learn 的 LogisticRegression 和来自 statsmodels.formula.api 的 smf.logit() 返回不同的结果

python - Tkinter.OptionMenu 未按预期工作

tkinter - ttk 创建和使用自定义主题

python - 如何使用 tkinter 颠倒笔记本中的选项卡标签

python - Tkinter 文本小部件变形列大小