简体   繁体   English

Python Tkinter网格布局问题

[英]Python tkinter grid layout problems

I'm creating a very simple UI using Tkinter and python, but I'm having trouble sizing GUI elements and using the grid format to place them correctly. 我正在使用Tkinter和python创建一个非常简单的UI,但是在调整GUI元素的大小和使用网格格式正确放置它们时遇到了麻烦。 Here's a first-order approximation of what I'm trying to have: 这是我要具有的一阶近似值:

图片

Here's the code I have so far. 这是我到目前为止的代码。 I keep getting close, but I don't think I really understand what I'm doing. 我一直很近,但是我不认为我真正了解自己在做什么。 Any help is much appreciated! 任何帮助深表感谢!

#User interface
root = Tk()
window_width = root.winfo_screenwidth()
window_height = root.winfo_screenheight()
root.geometry    ("%dx%d"%(window_width,window_height))

menu_bar = Menu(root)
menu = Menu(menu_bar, tearoff=0)
menu.add_command(label="Open", command = open_file)
menu.add_command(label="Save", command = save)
menu.add_separator()
menu.add_command(label="Quit", command = exit)
menu_bar.add_cascade(label="File",menu=menu)

root.config(menu=menu_bar)

#textbox is the window in which the code is written
textbox = Text(root, width=50, height = window_height/20+4)

#canvas is where the car will go
canvas_frame= Frame(root, width = window_width/1.5, height = window_height-200)
canvas_frame.configure(borderwidth=1.5,background='black')
canvas = Canvas(canvas_frame, width = window_width/1.5, height = window_height-200)

#console to print to
console = Text(root, width = int(window_width/1.5), height = 10)

run_button = Button(root, text = "Run", command = lambda:generate_program(textbox.get(1.0,END)))

clear_button = Button(root, text = "Clear text", command = clear)

#add them to frame
textbox.grid(row=0, column=0, rowspan=20, columnspan=10)
run_button.grid(row=21,column=0)
clear_button.grid(row=21,column=1)
canvas_frame.grid(row=0,rowspan=10,column=21,columnspan=25)
canvas.grid(row=0, rowspan=1, column=21, columnspan=25)
console.grid(row = 1, rowspan=1, column = 21, columnspan=25)

root.mainloop()

In my opinion, this is layout can be much easier with the pack geometry manager. 我认为,使用包装几何图形管理器可以简化布局。 One of the problems is that you are trying to make the width and the height of each widget fit in its place with rowspan and columspan options. 问题之一是,您试图通过rowspancolumspan选项使每个小部件的宽度和高度适合其位置。 Also, since canvas is inside a frame, you have to think that it is like inside a new window, so a simple call to canvas.grid() would be enough. 另外,由于canvas在框架内,因此您必须认为它就像在新窗口内一样,因此对canvas.grid()的简单调用就足够了。

However, with pack() you just have to put textbox , run_button and clear_button inside a new frame: 但是,使用pack()您只需要将textboxrun_buttonclear_button放在新框架中:

left_frame = Frame(root)
textbox = Text(left_frame, ...)
run_button = Button(left_frame, ...)
clear_button = Button(left_frame, ...)

canvas_frame= Frame(root, ...)
canvas_frame.configure(borderwidth=1.5,background='black')
canvas = Canvas(canvas_frame, ...)
console = Text(root, ...)

left_frame.pack(side=LEFT)
textbox.pack()
run_button.pack(side=LEFT)
clear_button.pack()
canvas_frame.pack()
canvas.pack()
console.pack()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM