使Tkinter框架填充垂直轴

时间:2018-12-28 06:34:14

标签: python python-3.x tkinter

我有此代码:

extendedConfigFile = "macOS 10.13.vmxf"

产生以下布局:Layout

但是我也希望框架也填充垂直轴。但是当我尝试使用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)

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

看起来扭曲了。无论如何,有没有扩展框架,但是使小部件看起来井井有条?

1 个答案:

答案 0 :(得分:0)

这是一个帮助您的小例子。这只是关于定位并在正确的容器中使用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

最后我要提醒您,所有变量,例如 mainFrame leftSide rightSide 和标签均为None (这是grid()的返回值)。请更正它们,以后您可能会遇到问题。