所以我正在创建一个GUI,我正在努力使它能够适当地适应屏幕。我已经绘制了一幅粗略的草图,我希望GUI的每个部分看起来像它的大小,所以我知道所有内容的粗略维度。
然而,我遇到的第一个问题是设置屏幕的左半部分。
所以左半部分由一个框架组成,我们称之为MainFrame
,它由2个框架组成,我们称之为LabelFrame
和ButtonFrame
MainFrame
需要385像素宽,460像素高。LabelFrame
应为375像素宽,115像素高。ButtonFrame
需要375像素宽,330像素高。我的问题是我不知道如何将这些尺寸设置为框架。
我已经尝试self.config(width = num, height = num)
显然用适当的值替换num,但是没有做任何事情。
我知道窗口本身有一个.geometry
方法,但我找不到tk.Frame的等价物
答案 0 :(得分:1)
使用grid_propagate(0)
或pack_propagate(0)
,依赖于正在使用的几何管理器。 0
只是False
,告诉tkinter关闭几何传播。
默认情况下,传播已启用,容器增长/缩小到足以容纳其内容。
我认为你想要的布局是这样的:
try:
import tkinter as tk
except ImportError:
import Tkinter as tk
root = tk.Tk()
MainFrame = tk.Frame(root, width=385, height=460, relief='raised', borderwidth=5)
LabelFrame = tk.Frame(MainFrame, width=375, height=115, relief='raised', borderwidth=5)
ButtonFrame = tk.Frame(MainFrame, width=375, height=330, relief='raised', borderwidth=5)
some_label = tk.Label(LabelFrame, text='Simple Text')
some_button = tk.Button(ButtonFrame, text='Quit', command=root.destroy)
for frame in [MainFrame, LabelFrame, ButtonFrame]:
frame.pack(expand=True, fill='both')
frame.pack_propagate(0)
for widget in [some_label, some_button]:
widget.pack(expand=True, fill='x', anchor='s')
root.mainloop()
和grid
经理仅在循环部分(注释sticky
和row/column configure
)中存在差异:
...
for frame in [MainFrame, LabelFrame, ButtonFrame]:
# sticky='nswe' acts like fill='both'
frame.grid(sticky='nswe')
frame.rowconfigure(0, weight=1)
frame.columnconfigure(0, weight=1)
frame.grid_propagate(0)
for widget in [some_label, some_button]:
# sticky='wse' acts like fill='x' + anchor='s'
widget.grid(sticky='wse')
root.rowconfigure(0, weight=1)
root.columnconfigure(0, weight=1)
...
答案 1 :(得分:1)
虽然我从不建议将UI构建为固定的像素大小,但如果您使用的是最简单的解决方案,则使用place
。 place
使您可以绝对控制窗口小部件的位置和大小,并且不会导致框架增大或缩小以适合其内容。
import tkinter as tk
root = tk.Tk()
root.geometry("800x480")
mainframe = tk.Frame(root, background="bisque")
labelframe = tk.Frame(mainframe, background="pink")
buttonframe = tk.Frame(mainframe, background="yellow")
mainframe.place(x=0, y=0, anchor="nw", width=385, height=460)
labelframe.place(x=0, y=0, anchor="nw", width=375, height=115)
buttonframe.place(x=0, y=116, anchor="nw", width=375, height=330)
root.mainloop()
如果您需要在具有不同分辨率或不同字体的设备上运行此操作,您将会遭受巨大的惩罚,因为重新排列所有内容将是一场噩梦。即使您定位到固定大小的外部窗口,我个人也建议在内部使用grid
或pack
并使用相对大小(例如:使主机填充一半的宽度和所有高度)
答案 2 :(得分:0)
我相信您正在使用columnconfigure()
和rowconfigure()
尝试这样的事情。
import tkinter as tk
class MintApp(tk.Frame):
def __init__(self, parent, *args, **kwargs):
tk.Frame.__init__(self, parent, *args, **kwargs)
self.root = parent
self.root.columnconfigure(0, weight = 0)
self.root.rowconfigure(0, weight = 0)
main_frame = tk.Frame(self.root, width = 385, height = 460, background = "black")
main_frame.columnconfigure(0, weight = 0)
main_frame.rowconfigure(0, weight = 0)
main_frame.grid(row = 0, column = 0)
label_frame = tk.Frame(main_frame, width = 375, height = 115, background = "blue")
label_frame.grid(row = 0, column = 0)
button_frame = tk.Frame(main_frame, width = 375, height = 330, background = "green")
button_frame.grid(row = 1, column = 0)
if __name__ == "__main__":
root = tk.Tk()
MyApp = MintApp(root)
tk.mainloop()