python-3.x - 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()
解决方案
推荐阅读
- angular - Angular 7 - 守卫的最佳方法是什么
- mongodb - $match 阶段在 MongoDB 聚合中没有给出任何输出
- angular - 使用 Bootstrap 4 进行布局
- ios - 根目录下的 Codable Handle 动态键
- swift - 如何在 Swift 中设置几个后续的倒数计时器
- c - 矩阵输入期间的分段错误
- angular - 如何将 Base64 Image 从一个组件调用到另一个组件
- android - 当第二次加载 GooglePlaceAutocomplete Fragment 时它崩溃了
- javascript - 在 react-csv 中下载 CSV 后调用函数
- javascript - 当 React 中的 state 不为 null 时更改样式组件