我有一个程序,可在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()
输出:
答案 0 :(得分:1)
这应该有效:
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()