首页 > 解决方案 > Python Tkinter。如何在固定大小的框架中居中标签?

问题描述

第一个文本行应该居中,图片和按钮也应该居中。第二个文本行应该左对齐。

如上所述,我想要一些元素居中(横向)。窗户应该是固定的。我之前尝试过使用列,但我不太明白如何将网格分配给框架并让标签遵守它。

import tkinter as tk
from PIL import Image, ImageTk

root = tk.Tk()


root.title("Programm zum setzen von Bildpunkten")
root.geometry("400x400")
root.resizable(width=False,height=False)



#Frames
Frame_1 = tk.Frame(master=root,bg="#c4c2bb",width=400,height=40)
Frame_2 = tk.Frame(master=root,bg="#c4c2bb",width=400,height=40)
Frame_3 = tk.Frame(master=root,bg="#c4c2bb",width=400,height=280)
Frame_4 = tk.Frame(master=root,bg="#c4c2bb",width=400,height=40)

Frame_1.grid_propagate(0)
Frame_2.grid_propagate(0)
Frame_3.grid_propagate(0)
Frame_4.grid_propagate(0)


#Main Containers layout
root.grid_columnconfigure(0, weight=1)
root.grid_rowconfigure(0, weight=2) 
root.grid_rowconfigure(1, weight=2) 
root.grid_rowconfigure(2, weight=1)
root.grid_rowconfigure(3, weight=0)

#Frame Position
Frame_1.grid(row=0,sticky="nw")
Frame_2.grid(row=1,sticky="ew")
Frame_3.grid(row=2,sticky="ew")
Frame_4.grid(row=3,sticky="new")

#Frame_1 Widgets
Start_message = tk.Label(master = Frame_1, text=Message_start,bg="#c4c2bb",fg="black")

#Frame_2 Widgets
Number_text = tk.Label(master = Frame_2, text=Number_Pictures,bg="#c4c2bb",fg="black")

#Frame_4 Widgets
#Start_button = tk.Button(master = Frame_4, text=Button_start_text,width=30,height=10,fg="white")

#Frame_1 Widget Position
Start_message.grid(row=0)
Anzahl_text.grid(row=0)

#Frame_3 Image, Dimension 200x200
Logo = Image.open("Logotest.jpg")
Logo = ImageTk.PhotoImage(Logo)
Logo_label = tk.Label(master = Frame_3,image=Logo)
Logo_label.grid(row=0)
   

root.mainloop()

标签: python-3.xtkinter

解决方案


推荐阅读