使用Tkinter动态调整窗口大小时的按钮大小
Python拥有许多丰富的库,用于创建和开发基于GUI的应用程序。Tkinter是用于创建基于GUI的应用程序的最常用的Python库之一。它具有许多功能,例如添加小部件和其他创建应用程序所需的必要属性。
按钮是一个可以为某些特定任务或事件分配的小部件。但是,要动态调整按钮小部件的大小或位置,我们可以使用Tkinter中的**Grid模块**配置其位置和布局。要动态调整按钮大小,可以使用**rowconfiguration()**和**coloumnconfiguration()**方法。
在Tkinter Grid系统中,有四个属性可用于调整任何小部件的大小。这些属性通常指的是方向,例如北、南、东和西。为了使按钮能够根据屏幕或窗口大小响应并动态调整大小,我们必须在其上使用**row**和**column**属性。
示例
#Importing the tkinter library
from tkinter import *
win= Tk()
win.title("Dynamically Resize Buttons")
win.geometry("700x500")
#Configure Rows and column
Grid.rowconfigure(win, 0,weight=1)
Grid.columnconfigure(win,0,weight=1)
#Create buttons
b1= Button(win, text= "C++")
b2= Button(win, text= "Java")
#Create List of buttons
bl= [b1, b2]
row_no=0
#Loop through all the buttons and configure it row-wise
for button in bl:
Grid.rowconfigure(win,row_no, weight=1)
row_no+=1
#Adjust the position in grid and make them sticky
b1.grid(row=0, column=0, sticky= "nsew")
b2.grid(row=1, column=0, stick= "nsew")
win.mainloop()输出
运行上述代码将生成输出,并在行顺序中水平显示两个按钮,可以根据屏幕或窗口大小动态调整大小。

广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP