我正在帮助清理高级设计项目的一些代码。学生们有一个 Python 应用程序,该应用程序使用 tkinter 创建用于库存控制和自动售货产品的 GUI。我不想发布大量代码,但希望描绘出我正在尝试做的事情。
内容描述如下: BinA、BinB、BinC、BinD 都是一样的,但是传入了唯一的变量,在一个叫做 pages.py 的新文档中创建了一个 bin 类。这些是在主程序底部创建的,带有它们的变量。然后我将它们与应用程序将浏览的其他页面一起放在一个framesList 中。列表中的所有其他帧目前都是代码中唯一的硬编码类,或者如果我们让它工作,我们将想要应用我正在努力解决的解决方案。
当 Bin(AD) 都具有唯一的硬编码自命名类时,除了我们确定的少数参数之外,这些类中具有相同的代码,屏幕导航按预期运行,但创建了 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:
但另一个是可以的,因为它们都是唯一的类。
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
页面.py
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])
BinA、BinB、BinC 和 BinD 都是同一个对象:Bin。将您的代码归结为最简单的术语,您期望它的输出是什么?
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?
试试看,你就会明白你的问题。
简单的解决方法是更换
BinA = pages.Bin
和
class BinA(pages.Bin): pass
从而创建一个独特的对象。
但这一点也不干净。要清理它,您将选择一个不同的指标来存储您的页面,同时转向现代继承风格并删除无用的控制器框架。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句