我已经尝试了几个小时来构建一个填充窗口的框架,其中一侧包含一个列表框,另一侧包含三个按钮:how I want the layout to be
相反,使用我当前的代码,结果看起来像this
任何人都可以指出我做错了什么,以及如何更改代码以使其达到我想要的方式?
当前代码:`
# Creates window
root = Tk()
root.title("MyMemory")
root.geometry("600x600")
root["bg"] = "sienna1"
# Frames the window
mainframe = ttk.Frame(root, padding="3 3 12 12")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
mainframe.columnconfigure(0, weight=1)
mainframe.rowconfigure(0, weight=1)
mainframe.pack(fill=BOTH, expand = True)
# Creates Listbox of existing subjects, from .txt files in directory of
program
subjects = Listbox(mainframe)
for file in glob.glob("*.txt"):
subjects.insert(0,file.split('.')[0])
subjects.grid(column = 0, row = 1, rowspan = 3)
# Creates a button for selecting the subject in the Listbox
select_subject = Button(mainframe, text = 'Select subject', command =
lambda: subject_menu(subjects, root))
select_subject.grid(column = 1, row = 0, sticky = (N))
# Creates a "make new subject" button
make_subject = Button(mainframe, text = 'Make new subject', command =
lambda: new_subject(root))
make_subject.grid(column = 1, row = 1, sticky = (N))
# Creates an "import subject" button
import_sub = Button(mainframe, text = 'Import a subject', command =
import_subject)
import_sub.grid(column = 1, row = 2, sticky = (N))
# Creates an "exit program" button
#close = Button(root, text = 'Exit program', command = ))
#close.pack(pady=20, padx = 20)
# Starts the loop
root.mainloop()`
答案 0 :(得分:0)
此相关问题可能对您有用: tkinter gui layout using frames and grid 1
将此应用于您的代码,您可能会得到以下内容:
import glob
from Tkinter import *
root = Tk()
root.title('Model Definition')
root.geometry('{}x{}'.format(600, 600))
# create all of the main containers
center = Frame(root, bg='gray2', width=590, height=590, padx=3, pady=3)
# layout all of the main containers
root.grid_rowconfigure(1, weight=1)
root.grid_columnconfigure(0, weight=1)
center.grid(row=1, sticky="nsew")
# create the center widgets
center.grid_rowconfigure(0, weight=1)
center.grid_columnconfigure(1, weight=1)
ctr_left = Frame(center, bg='blue', width=295, height=590, padx=50, pady=50)
ctr_right = Frame(center, bg='green', width=295, height=590, padx=70, pady=50)
ctr_left.grid(row=0, column=0, sticky="ns")
ctr_right.grid(row=0, column=2, sticky="ns")
# create right sub widgets
ctr_right.grid_rowconfigure(1, weight=1)
ctr_right.grid_columnconfigure(0, weight=1)
right_top = Frame(ctr_right, bg='yellow', width=295, height=194)
right_center = Frame(ctr_right, bg='red', width=295, height=194)
right_bottom = Frame(ctr_right, bg='purple', width=295, height=194)
right_top.grid(row=0, column=0, sticky="ew")
right_center.grid(row=1, column=0, sticky="ew")
right_bottom.grid(row=2, column=0, sticky="ew")
#
# make the Listbox and buttons
#
# Creates Listbox of existing subjects, from .txt files in directory of program
subjects = Listbox(ctr_left,
width=32,
height=30)
for this_file in glob.glob("*.txt"):
subjects.insert(0, this_file.split('.')[0])
subjects.grid(column=0, row=0, sticky="e")
# Creates a button for selecting the subject in the Listbox
select_subject = Button(right_top,
text='Select subject',
command=lambda: subject_menu(subjects, root),
width=20,
height=3)
select_subject.grid(column=0, row=0, sticky="e")
# Creates a "make new subject" button
make_subject = Button(right_center,
text='Make new subject',
command=lambda: new_subject(root),
width=20,
height=3)
make_subject.grid(column=0, row=0, sticky="e")
# Creates an "import subject" button
import_sub = Button(right_bottom,
text='Import a subject',
command=import_subject,
width=20,
height=3)
import_sub.grid(column=0, row=0, sticky="e")
root.mainloop()
请注意,向列表框或按钮添加宽度或高度使用的是字符而不是像素的度量。此代码还添加了颜色,以便更容易查看帧的位置,结果如下所示: