gpt-3.5-turbo-0301· OpenAI
WildChat-4.8M (AI2)
python tkinter 完成按钮大小一样,且均靠右排列的窗口
- User
python tkinter 完成按钮大小一样,且均靠右排列的窗口
- Assistant
以下是一个简单的Python Tkinter代码,可以创建大小相同且靠右对齐的按钮窗口:
pythonimport tkinter as tk root = tk.Tk() root.geometry('300x100') button_width = 80 button_height = 30 # 创建三个按钮 button1 = tk.Button(root, text='Button 1', width=button_width, height=button_height) button2 = tk.Button(root, text='Button 2', width=button_width, height=button_height) button3 = tk.Button(root, text='Button 3', width=button_width, height=button_height) # 将三个按钮添加到窗口中 button1.pack(side=tk.RIGHT) button2.pack(side=tk.RIGHT) button3.pack(side=tk.RIGHT) root.mainloop()解释:
-
使用
tkinter模块导入Tkinter库 -
创建一个根窗口
root -
使用
geometry()方法设置根窗口的大小为300x100 -
创建一个按钮宽度和高度设定为
80x30 -
创建三个按钮,并将它们分别命名为
button1,button2和button3 -
使用
pack()方法在按钮上设置右侧对齐,并将它们添加到窗口中
-