所以我有这个脚本,我用它来摆弄 tkinter gui 只是为了学习它,我有一个包含 3 个按钮的框架,我有行和列设置,所以它们没有间隙,它工作正常,直到我添加任何东西在下面的行中,发生的情况是第二个按钮和第三个按钮被拉到程序的另一侧,而第一个按钮只是粘在左侧,这真的很奇怪
好吧,我尝试更改导致问题的单选按钮上的填充,但它似乎根本没有做任何事情,所以我尝试向按钮本身添加一个列跨度,因为我认为这会让它粘住,但不
# Configuring all of the default GUI options
root = tk.Tk()
root.title("Steppazer")
root.geometry("450x450")
root.resizable(True, True)
yer = IntVar()
# Creating the main notebook for gui
my_notebook = ttk.Notebook(root)
my_notebook.pack(expand=1, fill='both')
# Creating all tabs and adding them to the main notebook
my_tab1 = ttk.Frame(my_notebook)
my_notebook.add(my_tab1, text="Lollll")
my_tab2 = ttk.Frame(my_notebook)
my_notebook.add(my_tab2, text="Lmaooo")
# Creating the frames
frame1 = LabelFrame(my_tab1, width=450, height=450, padx=5, pady=5, text="LabelFrameaz")
frame1.grid(padx=15, pady=15)
frame2 = LabelFrame(my_tab2, width=450, height=450, padx=5, pady=5)
frame2.grid(padx=15, pady=15)
# Creating Tab 1 Buttons
Button(frame1, text="Lollllerson", padx=5, pady=5, width=7, height=1)
Button.grid(row=1, column=0)
Button2(frame1, text="Ttheflip", padx=5, pady=5, width=7, height=1)
Button2.grid(row=1, column=2)
Button3(frame1, text="Wowza", padx=5, pady=5, width=7, height=1)
Button3.grid(row=1, column=3)
# Creating Radio Buttons
Radiobutton(frame1, text="Uno", variable=yer, value=1, padx=5, pady=5).grid(row=2, column=0)
Radiobutton(frame1, text="Dos", variable=yer, value=2, padx=5, pady=5).grid(row=2, column=1)
Radiobutton(frame1, text="Tres", variable=yer, value=3, padx=5, pady=5).grid(row=2, column=2)
root.mainloop()
不要使用通配符
from tkinter import *
使用 import tkinter as tk
,然后为每个小部件使用前缀 tk.
。
为每个小部件声明变量。例如
tk.Button
应为 btn = tk.Button
。
片段:
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.title("Steppazer")
root.geometry("450x450")
root.resizable(True, True)
yer = tk.IntVar()
:
:
:
# Creating the frames
frame1 = tk.LabelFrame(my_tab1, width=450, height=450, text="LabelFrameaz")
frame1.grid(padx=15, pady=5)
frame2 = tk.LabelFrame(my_tab2, width=450, height=450, padx=5, pady=5)
frame2.grid(padx=15, pady=15)
# Creating Tab 1 Buttons
btn = tk.Button(frame1, text="Lollllerson", bg='red', padx=5, pady=5, width=7, height=1)
btn.grid(row=1, column=0)
btn2 = tk.Button(frame1, text="Ttheflip", padx=5, pady=5, width=7, height=1)
btn2.grid(row=1, column=1)
btn3 = tk.Button(frame1, text="Wowza", padx=5, pady=5, width=7, height=1)
btn3.grid(row=1, column=2)
截图: