如何移动 Tkinter 按钮?
先决条件:在 tkinter 中创建一个按钮
Tkinter是在Python中开发 GUI(图形用户界面)最常用的库。它是Python附带的 Tk GUI 工具包的标准Python接口。由于 Tk 和 Tkinter 在大多数 Unix 平台以及 Windows 系统上都可用,因此使用 Tkinter 开发 GUI 应用程序变得最快和最简单。
这个模块没有内置于Python中。要安装此模块,请在终端中键入以下 pip 命令。
pip install tkintertable
方法:
- 在Python 2.x 中导入 tkinter 模块 # Tkinter。 (注大写 T)。
- 创建主窗口(root = Tk())。
- 在窗口中添加一个按钮。
- 放置按钮。
Tkinter模块中的按钮可以通过两种方式放置或移动到任意位置:
- 通过使用 place 方法。
- 并通过使用打包方法。
方法一:使用place方法
此方法用于将按钮放置在绝对定义的位置。
Syntax : button1.place(x=some_value, y=some_value)
Parameters :
- x : It defines the x-coordinate of the button’s position.
- y : It defines the y-coordinate of the button’s position.
下面是上述方法的实现:
Python3
# Importing tkinter module
from tkinter import *
# Creating a tkinter window
root = Tk()
# Initialize tkinter window with dimensions 300 x 250
root.geometry('300x250')
# Creating a Button
btn = Button(root, text = 'Click me !', command = root.destroy)
# Set the position of button to coordinate (100, 20)
btn.place(x=100, y=20)
root.mainloop()
Python3
# Importing tkinter module
from tkinter import *
# Creating a tkinter window
root = Tk()
# Initialize tkinter window with dimensions 300 x 250
root.geometry('300x250')
# Creating a Button
btn = Button(root, text = 'Click me !', command = root.destroy)
# Set a relative position of button
btn.pack(side=RIGHT, padx=15, pady=20)
root.mainloop()
Python3
# Importing tkinter module
from tkinter import *
# Creating a tkinter window
root = Tk()
# Initialize tkinter window
# with dimensions 300 x 250
root.geometry('300x250')
# Creating a Button
btn1 = Button(root, text = 'btn1 !',
command = root.destroy)
btn1.grid(row = 0, column = 0)
# Creating a Button
btn2 = Button(root, text = 'btn2 !', command = root.destroy)
btn2.grid(row = 1, column = 1)
# Creating a Button
btn3 = Button(root, text = 'btn3 !', command = root.destroy)
btn3.grid(row = 2, column = 2)
# Creating a Button
btn3 = Button(root, text = 'btn4 !', command = root.destroy)
btn3.grid(row = 4, column = 4)
root.mainloop()
Python3
# Importing tkinter module
from tkinter import *
# Importing random module
import random
# Creating a tkinter window
root = Tk()
# Initialize tkinter window with dimensions 300 x 250
root.geometry('300x250')
# Creating a Button
btn = Button(root, text = 'Click me !')
btn.pack()
# Defining method on click
def Clicked(event):
x = random.randint(50,250)
y = random.randint(50,200)
btn.place(x=x, y=y)
# bind button
btn.bind("" ,Clicked)
btn.pack()
root.mainloop()
输出:
方法二:使用pack方法
此方法用于在相对位置放置按钮。
Syntax : button1.pack(side=some_side, padx=some_value, pady=some_value)
Parameters :
- side : It defines the side where the button will be placed.
- padx : It defines the padding on x-axis from the defined side.
- pady : It defines the padding on y-axis from the defines side.
下面是上述方法的实现:
蟒蛇3
# Importing tkinter module
from tkinter import *
# Creating a tkinter window
root = Tk()
# Initialize tkinter window with dimensions 300 x 250
root.geometry('300x250')
# Creating a Button
btn = Button(root, text = 'Click me !', command = root.destroy)
# Set a relative position of button
btn.pack(side=RIGHT, padx=15, pady=20)
root.mainloop()
输出:
方法三:使用网格法
此方法用于在相对位置对按钮进行网格化。
Syntax : button.widget.grid( grid_options )
Parameters :
- column − The column to place widget in; default 0 (leftmost column).
- columnspan − How many columns widgetoccupies; default 1.
- ipadx, ipady − How many pixels to pad widget, horizontally and vertically, inside widget’s borders.
- padx, pady − How many pixels to pad widget, horizontally and vertically, outside v’s borders.
- row − The row to put widget in; default the first row that is still empty.
- rowspan − How many rowswidget occupies; default 1.
- sticky − What to try to to if the cell is larger than widget. By default, with sticky=”, widget is centered in its cell. sticky may be the string concatenation of zero or more of N, E, S, W, NE, NW, SE, and SW, compass directions indicating the sides and corners of the cell to which widget sticks.
下面是上述方法的实现:
蟒蛇3
# Importing tkinter module
from tkinter import *
# Creating a tkinter window
root = Tk()
# Initialize tkinter window
# with dimensions 300 x 250
root.geometry('300x250')
# Creating a Button
btn1 = Button(root, text = 'btn1 !',
command = root.destroy)
btn1.grid(row = 0, column = 0)
# Creating a Button
btn2 = Button(root, text = 'btn2 !', command = root.destroy)
btn2.grid(row = 1, column = 1)
# Creating a Button
btn3 = Button(root, text = 'btn3 !', command = root.destroy)
btn3.grid(row = 2, column = 2)
# Creating a Button
btn3 = Button(root, text = 'btn4 !', command = root.destroy)
btn3.grid(row = 4, column = 4)
root.mainloop()
输出:
下面是一个简单的应用程序,用于展示在 tkinter GUI 中随机移动按钮:
蟒蛇3
# Importing tkinter module
from tkinter import *
# Importing random module
import random
# Creating a tkinter window
root = Tk()
# Initialize tkinter window with dimensions 300 x 250
root.geometry('300x250')
# Creating a Button
btn = Button(root, text = 'Click me !')
btn.pack()
# Defining method on click
def Clicked(event):
x = random.randint(50,250)
y = random.randint(50,200)
btn.place(x=x, y=y)
# bind button
btn.bind("" ,Clicked)
btn.pack()
root.mainloop()
输出: