Python GUI编程:Tkinter模块窗口大小控制问题
在Python的Tkinter模块中,确实存在对窗口大小进行控制的问题。以下是几种常见的处理方式:
- 使用
geometry()
方法:window = tk.Tk()
window.geometry('500x300') # 设置窗口宽度和高度
- 设置窗口初始大小:
window = tk.Tk()
window.geometry("100x100") # 设置为一个默认的大小
使用
sizepolicy()
方法动态调整:import tkinter as tk
def resize(event):
window.geometry(f"{width.get()}x{height.get()}"})
root = tk.Tk()
frame = tk.Frame(root)
frame.pack()
width = tk.StringVar()
height = tk.StringVar()
label_width = tk.Label(frame, text="宽度: "))
label_height = tk.Label(frame, text="高度: "))
label_width.config(width=15)
label_height.config(width=15)
entry_width = tk.Entry(frame, textvariable=width))
entry_height = tk.Entry(frame, textvariable=height))
button_resize = tk.Button(root, text="调整大小", command=resize))
root.mainloop()
按照上述方式,你就可以在Tkinter中自由控制窗口的大小了。
还没有评论,来说两句吧...