Tkinter 中的窗口大小调整控制面板
本文将创建一个基于 GUI 的窗口大小调整控制面板,其中会提供一个可以按高度或宽度调整窗口大小的面板。
为了创建该程序,我们首先将创建一个帮助调整窗口大小的滑动条。滑块可以在 tkinter 的ttk 库中找到。我们将首先导入“ttk”。然后,我们将启动一个需要调整大小的新窗口。
让我们先在笔记本中导入所有必需的库,然后使用滑动条设计控制栏。
示例
# Import the required Libraries from tkinter import * from tkinter import ttk # Create Object win = Tk() # Set title win.title("Window Resizer") lab= Label(win, text="Window Resizer", font=('Poppins bold', 20)) #Define the geometry for the window or frame win.geometry("500x500") # Create a button to launch a new window launch_button = Button(win,text = "Launch") launch_button.pack(pady = 10) # Add Label Frames for width, height and both width_frame = LabelFrame(win, text = "Width") width_frame.pack(pady = 10) height_frame = LabelFrame(win, text = "Height") height_frame.pack(pady = 10) both_frame = LabelFrame(win, text = "Both") both_frame.pack(pady = 10) #Width Slider width_slider = ttk.Scale(width_frame,from_ = 100,to = 500,orient = HORIZONTAL,length = 200, value = 100) width_slider.pack(pady = 10, padx = 20) #Height Slider height_slider = ttk.Scale(height_frame, from_ = 100, to = 500, orient = VERTICAL,length = 200, value = 100) height_slider.pack(pady = 10, padx = 20) #Both Slider both_slider = ttk.Scale(both_frame, from_ = 100,to = 500, orient = HORIZONTAL,length = 200, value = 100) both_slider.pack(pady = 10,padx = 20) # Keep running the window win.mainloop()
创建滑动条和控制器的 GUI 后,我们将定义会在滑动条和窗口控件中调用的不同函数。
首先,我们将创建一个函数来打开一个新窗口,控制移动将出现在该窗口中。然后,我们将定义宽度、高度和两者同时的函数。
定义函数后,函数将如下所示 −
示例
# Import the required Libraries from tkinter import * from tkinter import ttk # Create Object win = Tk() # Set title win.title("Window Resizer") lab= Label(win, text="Window Resizer", font=('Poppins bold', 20)) #Define the geometry for the window or frame win.geometry("500x500") #Define Functions for all different events # Open New Window def launch_win(): global win1 win1 = Toplevel() win1.geometry("100x100") # Change width def change_width(x): win1.geometry(f"{int(width_slider.get())}x{int(height_slider.get())}") #Change height def change_height(x): win1.geometry(f"{int(width_slider.get())}x{int(height_slider.get())}") #Change both width and height def change_both(x): win1.geometry(f"{int(both_slider.get())}x{int(both_slider.get())}") # Create a button to launch a new window launch_button = Button(win,text = "Launch", command= launch_win) launch_button.pack(pady = 10) # Add Label Frames for width, height and both width_frame = LabelFrame(win, text = "Width") width_frame.pack(pady = 10) height_frame = LabelFrame(win, text = "Height") height_frame.pack(pady = 10) both_frame = LabelFrame(win, text = "Both") both_frame.pack(pady = 10) #Width Slider width_slider = ttk.Scale(width_frame,from_ = 100,to = 500,orient = HORIZONTAL,length = 200, command= change_height, value=100) width_slider.pack(pady = 10, padx = 20) #Height Slider height_slider = ttk.Scale(height_frame, from_ = 100, to = 500, orient = VERTICAL,length = 200,command= change_width, value=100) height_slider.pack(pady = 10, padx = 20) #Both Slider both_slider = ttk.Scale(both_frame, from_ = 100,to = 500, orient = HORIZONTAL,length = 200,command= change_both, value=100) both_slider.pack(pady = 10,padx = 20) #Keep Running the window or frame win.mainloop()
输出
运行上述代码将创建一个窗口大小调整器。
广告