首先 * 表示您导入所有内容:
from tkinter import *
这是没用的:
import tkinter
from tkinter import ttk, Checkbutton, Tk
其次,您使用此复选按钮多次写入相同的内容。尝试让您的工作更轻松,比如让 python 为您插入它们。
我不知道为什么,但来自 tkinter 的 Checkbutton 在自动排列方面有一些优势。
尝试改用 tkinter.ttk 中的 Checkbutton。
并尝试使用更多的 pack() 而不是(grid),这样安排它们会更好更容易。
这是我编写所有代码的方式,希望有用:)
# From tkinter we will use just Tk for the root and an IntVar because tkinter.ttk have not them :P
from tkinter import Tk, IntVar, Button
# From tkinter.ttik will import same checkbuttons becase thei will look more natural
# ttk will be much closer to default OS look them tkinter
# But the probblem here is you can not use bg, relief and such like this on ttk elements
# So if you want to add decorations on element try to import them from old/default tkinter :(((((
from tkinter.ttk import Frame, Checkbutton
root = Tk()
# Define two check buttons list
checkButtons_1 = []
checkButtons_2 = []
# We will set a number here what will be used to set the names on check buttons later
# Because will be used on more then one place I like to change it just one time next time
maxButtons = 10
# Try to append some names for them
# In programin the count start from 0 so we will increse the number with one to start from 1
for i in range(maxButtons):
# First of all we will add first half of the range
# Because some time we can get a float we try to avoid that by converting the division in a intiger
# But we will count with les 1 because: 10/2=5 so half is 5 but we will make i+1 so 5+1=6 is bigger the 5
if i <= int((maxButtons / 2) - 1):
checkButtons_1.append(f"Checkbox {i+1}")
# How we will add the rest of them
else:
checkButtons_2.append(f"Checkbox {i+1}")
class CheckBtn(Frame):
def __init__(self, parent=None, selected=[], side="top", expand=True, fill="x"):
Frame.__init__(self, parent)
self.vars = []
# For every itme new created
for item in selected:
# Because variabbles on checkbuttom are intiger (just 1 and 0) make an IntVar
var = IntVar()
# Make a little frame for every check box
frame = Frame(self)
frame.pack(side=side)
# Make the check bok
chk = Checkbutton(self, text=item, variable=var)
chk.pack(expand=expand, fill=fill, side=side)
# Save every variable state in a array list
self.vars.append(var)
# This stahe will be called later to see witch box are checked and witch not
def state(self):
return map((lambda var: var.get()), self.vars)
class gui:
def __init__(self, master):
self.master = master
# For the main screen we make two frame. One on top for the check buttons and one on bottom for the buttons
frameTop = Frame(master)
frameBottom = Frame(master)
# Now we make two frame for the checkbuttons. One for left and one for the right side
frameLeft = Frame(frameTop)
frameRight = Frame(frameTop)
# Here we will pack all frames. Here is important the order you pack, above you can make them on what order you want
frameTop.pack(expand=True, fill="x", side="top")
frameBottom.pack(expand=True, fill="x", side="bottom")
frameLeft.pack(expand=True, fill="x", side="left")
frameRight.pack(expand=True, fill="x", side="left")
# Here we create tne check button
# We define ch1 and ch2 because we will need to refer them to print the status later
ch1 = CheckBtn(frameLeft, checkButtons_1)
ch2 = CheckBtn(frameRight, checkButtons_2)
# Here we pack the buttons on screen
ch1.pack()
ch2.pack()
# We call lambda status here because our function will be after the button is pack
# So wil llook on all file for that function.
# With out that lambda function you need to put "seeStatus()" above the button.
# We not need to make btn = Button(frame,...) because we will not check the buttons anywhere for now
Button(frameBottom, text="Check status", command=lambda:seeStatus(),
bg="cyan", activebackground="cyan", relief="groove", bd=4
).pack(expand=True, fill="x", side="left")
# And for funn we add and this quit button :))
Button(frameBottom, text="Quit", command=root.quit,
bg="tomato", activebackground="tomato", relief="groove", bd=4
).pack(expand=True, fill="x", side="left")
def seeStatus():
print(list(ch1.state()), list(ch2.state()))
# If the this file is the main file then make this
# This basiclly check if you run the code from here or not.
# If not then will not execute this, you can import them to another file and will run from that file.
# Google it about. Is easy to undersnatnd.
if __name__ == "__main__":
gui(root)
root.mainloop()
但我不会回答你的问题,所以我只是给你一个完整的硬代码我认为......对不起。
问题在于你制作一个新框架,你必须以两种不同的方式完成。
# Like that
Frame(root).pack()
# Or you you refer them back to this frame you need to "give him a name"
frame = Frame(root)
frame.grid()
# THIS NEVER WILL WORK SO GOOD
frame = Frame(root).grid()
所以你的主要问题是你如何编写包/网格需要在两个不同的行中分开......
maincontent = Frame(mainWindow, borderwidth=5, relief='sunken', width=600, height=400)
maincontent.grid(row=0, column=0, padx=5, pady=5, sticky=NSEW)
content0 = Frame(maincontent, borderwidth=5, relief='sunken')
content0.grid(row=0, column=0, sticky=NS)
checkbox_0 = Checkbutton(content0, text="Checkbox 0", variable=checkbox0)
checkbox_0.grid(row=0, column=0, padx=5, pady=5)
#.......
content1 = Frame(maincontent, borderwidth=5, relief='sunken')
content1.grid(row=0, column=1, sticky=NS)
checkbox_5 = Checkbutton(content1, text="Checkbox 5", variable=checkbox5)
checkbox_5.grid(row=0, column=0, padx=5, pady=5)
#.......