首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python从带有参数的列表中创建tkinter类的唯一实例。

Python从带有参数的列表中创建tkinter类的唯一实例。
EN

Stack Overflow用户
提问于 2020-12-01 20:16:54
回答 1查看 97关注 0票数 0

我正在帮助清理一个高级设计项目的代码。学生们有一个python应用程序,它使用tkinter创建一个用于库存控制和销售产品的GUI。我不想发布大量的代码,但希望描述一下我想要做的事情。

对以下内容的描述: BinA、BinB、BinC和BinD都是相同的,但是通过输入唯一的变量,在一个名为pages.py的新文档中创建了一个bin类。这些都是在主程序底部创建的,并带有它们的变量。然后,我将它们与应用程序将导航的其他页面放在一个framesList中。列表中的所有其他框架目前都是代码中唯一的硬编码类,或者我们希望应用我正在努力解决的解决方案,如果我们让它正常工作的话。

当Bin( and )都有唯一的硬编码自命名类,其中除了我们识别的几个参数外,它们的代码是相同的,屏幕导航按预期操作,但是创建4个pages.bin实例并遍历帧列表只会导致只包含最后创建的变量的4个pages.bin。您可以看到在for F in frameList:之后调用了show self.show_frame(BinA),但是LinkList中的第一个元素是一个字符串,它在屏幕上填充一个标签。此标签只显示Pages.bin的最后一个框架创建的内容。类型。另一个屏幕都是正确创建和功能的。只是导航回屏幕Bin(?)在这种情况下,行为总是类似于BinD。如果我以相反的顺序创建它们,D,C,B,A,那么BinA是所有四个的结果。

我怀疑我们只是在创建pages.Bin的一个实例,并且只是在for F in frameList:的每个循环中覆盖它,但是另一个没有问题,因为它们都是唯一的类。

代码语言:javascript
复制
import tkinter as tk
from tkinter import *
import random
import time
import csv
import openpyxl
import pages

#get Display Size
root = tk.Tk()
screen_width = root.winfo_screenwidth() #1440
screen_height = root.winfo_screenheight() #900
#Close the popup
root.destroy()

print('Width: %i px, height: %i px' %(screen_width, screen_height))


#Create the main Application
class CMGUI(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        tk.Tk.wm_title(self, "CMA: AutoBox")
        tk.Tk.wm_resizable(self,0,0)
        tk.Tk.wm_overrideredirect(self,False)#True) //Use this on the final
        container = tk.Frame(self)       
        container.pack(side="top", fill="both", expand=True)
        container.grid_columnconfigure(0, weight=1)
        container.grid_rowconfigure(0, weight=1)
        

        self.frames = {}

        #Declart the Objects

        # Put them in a List
        frameList =    [[BinA       ,BinA_linksList],
                        [BinB       ,BinB_linksList],
                        [BinC       ,BinC_linksList],
                        [BinD       ,BinD_linksList],
                        [HomePage   ,None],
                        [PPE        ,None],
                        [Weld       ,None],
                        [Misc       ,None],
                        [Set        ,None],
                        [PPE2       ,None],
                        [Weld2      ,None],
                        [Misc2      ,None],
                        [AllItems   ,None],
                        [BinType    ,None],
                        [Inventory  ,None],
                        [LoginPage  ,None]]

        for F in frameList:             
        #   frame = F(parent=container, controller=self)
            frame = F[0](container, self, F[1])
            self.frames[F[0]] = frame
            frame.grid(row=0, column=0, sticky="nsew")
            frame.configure(bg='deepskyblue')


        self.show_frame(BinA) 
....
class LoginPage(tk.Frame):
     def __init__(self, parent, controller,*args):
        tk.Frame.__init__(self, parent)
        ....

class HomePage(tk.Frame):
    def __init__(self, parent, controller,*args):
        tk.Frame.__init__(self, parent)
        ....

class AllItems(tk.Frame):
    def __init__(self, parent, controller,*args):
        tk.Frame.__init__(self, parent)
        .....

ALLOT OF other CODE creating the other page classes like BinType, HomePage, PPE, and more
....

#Create 4 pages for bins
# Bin? = pages.Bin
# Bin?_linkList = ["Label"  ,return_Link ,Link_1    ,Link_2     ,Link_3     ,Link_4]

BinA = pages.Bin
BinA_linksList = ["Bin A"   ,BinType    ,HomePage   ,PPE        ,PPE        ,PPE]

BinB = pages.Bin
BinB_linksList = ["Bin B"   ,BinType    ,PPE        ,HomePage   ,PPE    ,PPE]

BinC = pages.Bin
BinC_linksList = ["Bin C"   ,BinType    ,PPE        ,PPE        ,HomePage   ,PPE]

BinD = pages.Bin
BinD_linksList = ["Bin D"   ,BinType    ,PPE        ,PPE        ,PPE    ,HomePage]


w = str(screen_width)
h = str(screen_height)
z = str("+0+0")
screenres = w+'x'+h+z

app = CMGUI()
app.geometry(screenres)
app.mainloop()
root.destroy

pages.py

代码语言:javascript
复制
import tkinter as tk

class Bin(tk.Frame):

    def __init__(self, parent, controller, data):
     
        print(data[0])
        tk.Frame.__init__(self, parent)
        
        # Header Label        
        tk.Label(self, font=('times', 50, 'bold'), 
            text=data[0], fg="deepskyblue", bd=20, anchor='center', 
            bg="black", relief="ridge").place(anchor = "n", relx = .5, y =0)
        
        # Footer Label
        tk.Label(self, font=('times',22,'bold'), 
            text="Page 1", bg="deepskyblue", fg="black").place(anchor = "s", relx = .5, rely = .95)

        #Buttons:
        #mylist =   [["Title"]                  , page      , "anchor"  , %x    ,  %y   ],
        btnList =   [["Search By\n Bin Type"    , data[0]   , "ne"      , .95   , .05   ],
                    ["Back to\n Categories"     , data[1]   , "nw"      , .05   , .05   ]]
                    
        for b in btnList:
            tk.Button(self, padx=5, pady=5, bd=16, fg="white", font=('times',24,'bold'), width=8, height=2,
                  text=b[0], bg="black", relief="raised",
                  command = lambda i=b[1]: controller.show_frame(i)).place(anchor = b[2],relx = b[3], rely = b[4])

        btnList2 =   [["Item 1"   , data[2]   , "se"      , .5   , .5   ],
                     ["Item 2"    , data[3]   , "sw"      , .5   , .5   ],
                     ["Item 3"    , data[4]   , "ne"      , .5   , .5   ],
                     ["Item 4"    , data[5]   , "nw"      , .5   , .5   ]]
                
        for b in btnList2:
            tk.Button(self, padx=5, pady=5, bd=16, fg="white", font=('times',24,'bold'), width=9, height=3,
                  text=b[0], bg="black", relief="raised",
                  command = lambda i=b[1]: controller.show_frame(i)).place(anchor = b[2],relx = b[3], rely = b[4])
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-12-01 21:00:44

BinA、BinB、BinC和BinD实际上都是同一个对象: Bin。要将代码简化为最简单的术语,您期望它的输出是什么?

代码语言:javascript
复制
data = 'spam'
BinA = data
BinB = data
BinC = data
BinD = data

frames = {}
for F in (BinA, BinB, BinC, BinD):
    frames[F] = 42

print(frames) # what do you expect this to print? 

试试看,你就会明白你的问题了。

简单的解决办法是替换

代码语言:javascript
复制
BinA = pages.Bin

使用

代码语言:javascript
复制
class BinA(pages.Bin): pass

从而创建一个唯一的对象。

但这一点都不干净。要清理它,您需要选择一个不同的度量来存储页面,同时移动到现代的继承样式,并删除无用的控制器框架。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65097879

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档