python - Tkinter 管理帧
问题描述
我在我的一个程序中遇到了障碍。以下是我的代码。我创建了一个带有多个框架的 tkinter 窗口应用程序。我能够正确地设置它们,框架内有框架。但是,我不知道为什么添加标签小部件会修改框架的大小。建议将不胜感激。
在以下代码中,请查看取消注释这两个标签小部件之间的区别。
import tkinter as tk
def main():
print ('Beginning')
app = tk.Tk()
size_x = app.winfo_screenwidth()
size_y = app.winfo_screenheight()
app.title('Color Mixer')
app.geometry("%dx%d+0+0" % (size_x, size_y))
# This disables resizing windows.
#app.configure(background = 'yellow')
print ('Beginning')
#Main containers
top_frame = tk.Frame(app, bg = 'yellow', width=size_x, height=int(size_y/3), padx = 3, pady=3)
bottom_frame = tk.Frame(app, bg='green', width=size_x, height=int(2*size_y/3), padx=3, pady=3)
# layout all of the main containers
app.grid_rowconfigure(0, weight=1)
app.grid_columnconfigure(0, weight=1)
top_frame.grid(row=0, sticky="nsew")
bottom_frame.grid(row=1, sticky="nsew")
# create the widgets for the top frame
model_label = tk.Label(top_frame, font = ('Times', str(int(size_x*0.014)), 'bold'), bg = 'yellow' , text='Pick a Color Model')
model_label.grid(row=0, column = 0)
v = tk.IntVar()
tk.Radiobutton(top_frame,
text="RYB Color Space",
indicatoron=0,
padx=35,
font=('Times',str(int(size_x * 0.0075)), 'normal'),
variable=v,
value=1).grid(row=1, column=0)
tk.Radiobutton(top_frame,
text="CMYK Color Space",
indicatoron=0,
padx=20,
font=('Times', str(int(size_x * 0.0075)), 'normal'),
variable=v,
value=2).grid(row=2, column=0)
top_frame.grid_rowconfigure(0, weight=1)
top_frame.grid_rowconfigure(1, weight=1)
top_frame.grid_rowconfigure(2, weight=1)
top_frame.grid_columnconfigure(0, weight = 1)
# Bottom Frame - Dividing into two frames.
bottom_left = tk.Frame(bottom_frame, bg='blue', width=int(0.5*size_x), height=int(2/3*size_y), padx = 3, pady =3)
bottom_right = tk.Frame(bottom_frame, bg='red', width=int(0.5*size_x), height=int(2/3*size_y), padx=3, pady=3)
bottom_left.grid(row=0, column=0, sticky="nsew")
bottom_right.grid(row=0, column=1, sticky="nsew")
# ryb_note = tk.Label(bottom_left, font=('Times', str(int(size_x * 0.005)), 'normal'), bg='red', text='NOTE')
# ryb_note.grid(row=0, column=0)
# ryb_red = tk.Label(bottom_left, font=('Times', str(int(size_x * 0.005)), 'normal'), bg='red', text='Red')
# ryb_red.grid(row=1, column=0)
bottom_frame.grid_rowconfigure(0, weight = 1)
bottom_frame.grid_columnconfigure(0, weight = 1)
bottom_frame.grid_columnconfigure(1, weight = 1)
app.mainloop()
if __name__ == '__main__':
main()
解决方案
您可以通过添加几个方法调用来防止两个框架受到其内容的影响,grid_propagate()
如下所示。这是一些文档。
...
# Bottom Frame - Dividing into two frames.
bottom_left = tk.Frame(bottom_frame, bg='blue', width=int(0.5*size_x), height=int(2/3*size_y), padx = 3, pady =3)
bottom_right = tk.Frame(bottom_frame, bg='red', width=int(0.5*size_x), height=int(2/3*size_y), padx=3, pady=3)
bottom_left.grid(row=0, column=0, sticky="nsew")
bottom_right.grid(row=0, column=1, sticky="nsew")
bottom_left.grid_propagate(0) # Added
bottom_right.grid_propagate(0) # Added
...
推荐阅读
- javascript - 带有回调的循环中的 JavaScript 异步
- codeigniter - 如何在codeignitier中阻止输入文本字段中的表情符号
- r - 使用数据框中的两个预先存在的变量创建一个指示特定条件的新变量
- oracle - 当我使用外部表时,Shell 脚本在内部调用另一个 shell 脚本并且不将输出返回到主脚本
- javascript - 使用一条线将画布分成两个新画布
- android - 面部认证,如 Android 中的手指认证
- ios - 如何在 iOS 10 中不使用 healthkit 获取睡眠数据
- java - 错误:无法设置 io.appium.java_client.MobileElement 字段在框架中抛出
- bash - 如何在 bash 中进行搜索、输入文本和替换一行?
- asp.net - 如何根据我在同一页面中使用删除按钮从查询中检索到的记录数来实现多个详细信息视图控件?