使用tkinter-in-pack函数在循环中并排放置两个标签



我是编程新手,在tkinter中,我用网格函数制作了两个窗口,在循环中,我希望使用pack函数将标签和条目并排,尝试了所有选项,唯一有效的是普通的pack,但它将输入定位在标签后面,而不是它的旁边如何修复

`

from csv import Dialect
import tkinter as tk
from PIL import ImageTk
from tkmacosx import Button
import sqlite3
import csv 

# define variables
# bg = "#03dffc"
# bg = "#022226"
bg = "#5bc6de"
#define function that write data to csv file or excel 
def write_to_csv(all_names):
with open("inventory.csv" , "w") as f :
w = csv.writer(f, dialect="excel")
for name in all_names:
w.writerow(name)

#define function to clear the previous widget when load the new one 
def clear_widget(frame):
for widget in frame.winfo_children():
widget.destroy()
#function to fetch data from database 
def fetch_db():
connection = sqlite3.connect("data/inventory.db")
cursor = connection.cursor()
cursor.execute("SELECT NAME FROM inventory1 ORDER BY RANDOM() LIMIT 10 ;")
all_names = cursor.fetchall()
connection.close()
return all_names
# def pre_process(all_names):
#     names = all_names
def load_frame1():
#destroy other fram
clear_widget(frame2)
#adjust frame order
frame1.tkraise()
#to avoid any widget corraption 
frame1.pack_propagate(False)
# add logo image as a widget to frame1
logo_img = ImageTk.PhotoImage(file="assets/pharmacy3.png")
logo_widget = tk.Label(frame1,image=logo_img, bg=bg)
logo_widget.image = logo_img
logo_widget.pack(pady=20)
#add text as instraction so text as a widget 
tk.Label(
frame1,
text="Press Generate for random product list to review and print",
bg = bg ,
fg = "white",
font=("TKMenuFont", 14)
).pack(pady=20)
#create a button widget 
Button(
frame1,
text="Generate",
bg = "#155361",
fg = "white" ,
font=("TKMenuFont" , 22 ),
activebackground="#1898b5",
activeforeground="black",
cursor="hand",
borderless = 1 ,
command=lambda:load_frame2()
).pack(pady=20)


def load_frame2():
#destroy other fram
clear_widget(frame1)
#adjust frame order
frame2.tkraise()
#retuen fetched data 
all_names = fetch_db()
# names = pre_process(all_names)
logo_img = ImageTk.PhotoImage(file="assets/pharmacy2.png")
logo_widget = tk.Label(frame2,image=logo_img, bg=bg)
logo_widget.image = logo_img
logo_widget.pack(pady=20)
for i in all_names :
tk.Label(
frame2,
text=i,
bg = "#155361" ,
fg = "white",
font=("TKMenuFont", 10),
).pack()
tk.Entry(
frame2 ,
width = 10 ,
).pack()
Button(
frame2,
text="Back",
bg = "#155361",
fg = "white" ,
font=("TKMenuFont" , 22 ),
activebackground="#1898b5",
activeforeground="black",
cursor="hand",
borderless = 1 ,
command=lambda:load_frame1()
).pack(pady=20)
Button(
frame2,
text="Save file",
bg = "#155361",
fg = "white" ,
font=("TKMenuFont" , 22 ),
activebackground="#1898b5",
activeforeground="black",
cursor="hand",
borderless = 1 ,
command=lambda:write_to_csv(all_names)
).pack(pady=20)

# initiallize app
root = tk.Tk()
# add a title to the screen
root.title("Hafez Pharmacy Inventory Check")

root.eval("tk::PlaceWindow . upper")
# another approch to center the screen
# x = root.winfo_screenwidth() // 2
# y = int(root.winfo_screenheight() * 0.2)
# root.geometry("500x500+" + str(x) + "+" + str(y))
# first window called frame1
frame1 = tk.Frame(root, width=500, height=500, bg=bg)
frame2 = tk.Frame(root, bg=bg)
# frame1.grid(row=0, column=0)
#instead of coping the line 
#instead we will create a for loop 
for fram in (frame1, frame2):
fram.grid(row=0, column=0, sticky="nesw")
load_frame1()

# run app
root.mainloop()

`

我尝试了放置功能和全包功能选项,我尝试在标签和条目上使用网格,但它在框架或窗口中使用,所以给我错误

我想把条目放在右边标签后面,而不是像这个一样放在底部

产品名称[提交的输入]产品名称[输入文件]

您可以使用.grid()将这些标签和条目放入新的帧中,然后将该帧打包到frame2:中

def load_frame2():
...
input_frame = tk.Frame(frame2, bg="#155361", padx=5, pady=5)
input_frame.pack()
for i, name in enumerate(all_names):
tk.Label(
input_frame,
text=name,
bg="#155361",
fg="white",
font=("TKMenuFont", 10),
).grid(row=i, column=0, sticky='ew')
tk.Entry(
input_frame,
width=10,
).grid(row=i, column=1)
...

最新更新