gpt4 book ai didi

python - 使 Tkinter 框架填充垂直轴

转载 作者:太空宇宙 更新时间:2023-11-03 21:24:27 26 4
gpt4 key购买 nike

我有这个代码:

self.master= master
self.master.title('Student Tracker')
self.master.configure(bg='#007534')
self.photo = PhotoImage(file = 'Logo.gif')
self.SchoolLogo = Label(self.master, image = self.photo,bg = 'white').grid(row=0,column=0,columnspan=2,sticky=W+E)
mainFrame = Frame(self.master).grid(sticky=N+E+S+W)
leftSide = Frame(mainFrame,bg='#007534').grid(sticky=N+E+S+W,row=1,column=0,rowspan=5)
rightSide=Frame(mainFrame,bg='#A8D59D').grid(row=1,column=1,rowspan=5,sticky=N+E+S+W)
welcomeLabel= Label(leftSide,text='Welcome', fg='white',bg='#007534').grid(row=2,column=0,sticky=N)
welcome2=Label(rightSide,text='Hello',fg='Black').grid(row=2,column=1)
attendanceBtn=Button(leftSide,text='Attendance',command=self.attendance,bg='white',fg='#007534').grid(row=3,column=0)
gradeBtn=Button(leftSide,text='Grades',command=self.attendance,bg='white',fg='#007534').grid(row=4,column=0)
self.master.grid_columnconfigure(0, weight=1)
self.master.grid_columnconfigure(1, weight=4)

生成此布局:Layout

但我希望框架也能填充垂直轴。但是当我尝试使用 .grid_rowconfigure(0, Weight=1)

我得到了第二个布局:Second Layout

看起来扭曲了。有没有办法扩展框架,但使小部件看起来井井有条?

最佳答案

这里有一个小例子可以帮助您。它只是在右侧容器中调整位置并使用 grid_rowconfigure()grid_columnconfigure()

import tkinter as tk

root = tk.Tk()
root.configure(bg='#007534')
mainFrame = tk.Frame(root).grid(sticky="nsew")
leftSide = tk.Frame(mainFrame, bg='#007534')
leftSide.grid(sticky="nsew", row=0, column=0)
rightSide = tk.Frame(mainFrame, bg='#A8D59D')
rightSide.grid(row=0, column=1, sticky="nsew")
tk.Label(leftSide,text='Welcome', fg='white',bg='#007534').grid(row=0, column=0, sticky="n")
tk.Label(rightSide,text='Hello',fg='Black').grid(row=0, column=0)
tk.Button(leftSide,text='Attendance', bg='white', fg='#007534').grid(row=1, column=0)
tk.Button(leftSide,text='Grades', bg='white', fg='#007534').grid(row=2, column=0)
root.grid_columnconfigure(0, weight=1)
root.grid_columnconfigure(1, weight=4)
leftSide.grid_columnconfigure(0, weight=1)
rightSide.grid_columnconfigure(0, weight=1)
root.grid_rowconfigure(0, weight=1)
root.mainloop()

enter image description here

最后我应该提醒你,所有的变量,如 mainFrameleftSiderightSide 和标签都是 None (这是 grid() 的返回值)。请纠正它们,稍后您可能会遇到问题。

关于python - 使 Tkinter 框架填充垂直轴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53954579/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com