gpt4 book ai didi

python - 如何使用Python Tkinter在一行中显示几个标签而在下一行中显示几个标签?

转载 作者:行者123 更新时间:2023-12-02 02:39:57 24 4
gpt4 key购买 nike

我有一个程序在GUI屏幕上显示标签,但全部显示在一行中。我想在一行的第一个循环中显示所有标签,并在另一行显示第二个循环中的所有标签。代码如下:

from tkinter import *
import tkinter as tk

win = Tk()
win.title("Labels")
win.geometry("800x600+50+50")
win.config(bg='white')

label1=Label(win, text="All Labels", font=("Calibri",24,"bold"), bg='white', borderwidth=1, relief="solid")
label1.pack(pady=15)

for a in range(10):
pre1=Label(win, text=("PIN",a), font=("Calibri",12), bg="white", borderwidth=1, relief="solid")
pre1.pack(side=tk.LEFT)

for b in range(10):

if (b%2==0):
pre2=Label(win, text="OFF", font=("Calibri",12), bg="white", borderwidth=1, relief="solid")
pre2.pack(side=tk.LEFT)
else:
pre3=Label(win, text="ON", font=("Calibri",12), bg="white", borderwidth=1, relief="solid")
pre3.pack(side=tk.LEFT)

win.mainloop()

输出:

enter image description here

最佳答案

这应该工作:

from tkinter import *
import tkinter as tk

win = Tk()
win.title("Labels")
win.geometry("800x600+50+50")
win.config(bg='white')

label1=Label(win, text="All Labels", font=("Calibri",24,"bold"), bg='white', borderwidth=1, relief="solid")
label1.pack(pady=15)

lblframe = tk.Frame(win)
for a in range(10):
pre1=Label(lblframe, text=("PIN",a), font=("Calibri",12), bg="white", borderwidth=1, relief="solid")
pre1.grid(row=0, column=a)

for b in range(10):

if (b%2==0):
pre2=Label(lblframe, text="OFF", font=("Calibri",12), bg="white", borderwidth=1, relief="solid")
pre2.grid(row=1, column=b)
else:
pre3=Label(lblframe, text="ON", font=("Calibri",12), bg="white", borderwidth=1, relief="solid")
pre3.grid(row=1, column=b)

lblframe.pack()

win.mainloop()

Preview

关于python - 如何使用Python Tkinter在一行中显示几个标签而在下一行中显示几个标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60277983/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com