如何使用 for 循环在 tkinter 中用空白帧填充屏幕设计的其余部分? [英] How do I fill the rest of my screen design with blank frames in tkinter using for loop?

查看:54
本文介绍了如何使用 for 循环在 tkinter 中用空白帧填充屏幕设计的其余部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个以购物为灵感的自动售货机用户界面.目标是使用 for 循环和 2D 列表用框架填充我的框架,然后将按钮和标签打包到其上.代码如下:

I am trying to make a shopping-inspired vending machine UI. The goal is to fill up my frame with frames using for loop and 2D list, then pack on buttons and labels onto it. Codes are as followed:

    frames = []
    frame_order = []
    num = 0

    for x in range(5):
        frames.append([])
        for y in range(5):
            frames[x].append(5)

    for x in range(5):
        for y in range(5):
            frames[x][y] = Frame(store_canvas, width=1520 / 5, height=1030 / 5, bd = 2, relief = SOLID)
            frames[x][y].grid(row=y, column=x)
            frames[x][y].pack_propagate(False)
            frame_order.append(frames[x][y])

这将重新运行一个 SQL 查询数据库,该数据库将返回一个作为 list[] 的项目列表.然后填充 2D list

This will reun an SQL query database that will return with an item list as a list[].Then fill up the frames inside the 2D list

retrieve_tiem() 

我还在有其他过滤器的一侧设置了另一个框架.问题是,如果过滤器查询 SQL 并返回一个小于网格大小(5x5,总共 25 个项目)的 item list,那么 for 循环将不会运行,因为它无法填充.为了避免这种情况,我尝试使用 TryExcept 但循环只会用按钮填充剩余空间.

I have also set up another frame on the side that has other filters. the problem is that if the filter query SQL and returns with an item list less than the grid size (5x5, a total of 25 items) then the for loop won't run as it cannot populate. to avoid this, I tried to use Try and Except but the loop would just fill the remaining space with buttons instead.

for frame in frame_order:
    try:
        Button(frame, anchor='nw', height = 9, width = 35, font = 20).pack()
        Label(frame, text=str(Item_list[num]), anchor='nw', font = 20, width = 35, bg = 'darkgreen', fg = 'yellow' ).pack()
        num += 1
    except:
        pass

有没有办法避免这种情况?喜欢在项目列表用完项目时不创建按钮或用 '*blank*' 填充列表的其余部分,直到列表达到框架可以容纳的项目总数?如果可行,我也对其他方法持开放态度.感谢所有答案,请放轻松,因为我仍在学习 python 和 SQL:)

Is there a way to avoid this? Like not creating the button when the Item list run out off item or fill the rest of the list with '*blank*' until the list reach the total amount of item that the frame can hold? I am also open to other methods too if it works. All answer are appreciated, please go easy on me as I am still learning python and SQL:)

推荐答案

实际上可以使用一维列表代替二维列表.以下是基于您的代码的示例:

You can actually use 1-D list instead of 2-D list. Below is an example based on your code:

from tkinter import *
import random

root = Tk()

store_canvas = Frame(root)
store_canvas.pack()

# create the 25 frames
ROWS = COLS = 5
MAX_ITEMS = ROWS * COLS
frames = []
for i in range(MAX_ITEMS):
    frames.append(Frame(store_canvas, width=1520/COLS, height=1030/ROWS, bd=2, relief=SOLID))
    frames[-1].grid(row=i//COLS, column=i%COLS)
    frames[-1].pack_propagate(False)

# function to simulate retrieving data from database table
def retrieve_tiem():
    return [f"Item #{i+1}" for i in range(random.randint(1,MAX_ITEMS))]

# function to show the retrieved items
def update_list():
    Item_list = retrieve_tiem()
    label_font = ("Arial", 20)
    for i, frame in enumerate(frames):
        for w in frame.winfo_children():
            w.destroy()
        if i < len(Item_list):
            item = Item_list[i]
            Button(frame).pack(fill="both", expand=1)
            Label(frame, text=item, font=label_font, bg="darkgreen", fg="yellow").pack(fill="x")

update_list()
root.bind("<F5>", lambda e: update_list())
root.mainloop()

这篇关于如何使用 for 循环在 tkinter 中用空白帧填充屏幕设计的其余部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆