将循环中的小部件实例化到不同的python/tkinter框架
我正在用python创建一个简单的GUI产品计算器。我用的是Tkinter,有一个主框架,里面有10个标签。我已经创建了所有的条目和函数来做我们需要的计算,并且一切正常。我的问题是,我希望在每个选项卡行10-行19上都有这些条目和标签。我可以为每个选项卡手动重新创建代码,但这似乎不太符合Python。我所希望的是能够将这段代码放入一个循环中,该循环将重命名每行的变量名,并通过更改网格方法中的参数将对象放置到不同的选项卡框架中。我希望我足够清楚,我是一个新手,希望能很好地掌握这门语言。我希望能够在所有变量名的末尾用一个不同的数字来重复这段代码,串接会起作用吗 这是我的密码将循环中的小部件实例化到不同的python/tkinter框架,python,loops,for-loop,tkinter,widget,Python,Loops,For Loop,Tkinter,Widget,我正在用python创建一个简单的GUI产品计算器。我用的是Tkinter,有一个主框架,里面有10个标签。我已经创建了所有的条目和函数来做我们需要的计算,并且一切正常。我的问题是,我希望在每个选项卡行10-行19上都有这些条目和标签。我可以为每个选项卡手动重新创建代码,但这似乎不太符合Python。我所希望的是能够将这段代码放入一个循环中,该循环将重命名每行的变量名,并通过更改网格方法中的参数将对象放置到不同的选项卡框架中。我希望我足够清楚,我是一个新手,希望能很好地掌握这门语言。我希望能够在
from Tkinter import *
from ttk import *
import time
class supervisorcalc:
def __init__(self, master):
self.notebook = Notebook(master)
self.line10 = Frame(self.notebook);
self.notebook.add(self.line10, text="Line 10")
self.line11 = Frame(self.notebook);
self.notebook.add(self.line11, text="Line 11")
self.line12 = Frame(self.notebook);
self.notebook.add(self.line12, text="Line 12")
self.line13 = Frame(self.notebook);
self.notebook.add(self.line13, text="Line 13")
self.line14 = Frame(self.notebook);
self.notebook.add(self.line14, text="Line 14")
self.line15 = Frame(self.notebook);
self.notebook.add(self.line15, text="Line 15")
self.line16 = Frame(self.notebook);
self.notebook.add(self.line16, text="Line 16")
self.line17 = Frame(self.notebook);
self.notebook.add(self.line17, text="Line 17")
self.line18 = Frame(self.notebook);
self.notebook.add(self.line18, text="Line 18")
self.line19 = Frame(self.notebook);
self.notebook.add(self.line19, text="Line 19")
self.notebook.grid(row=0,column=0)
###functions###
def cyclecnt(*args):
cyclecount = int(self.cyccnt.get())
molds = int(self.vj.get())
cyccount = cyclecount * molds
self.cyc.set(cyccount)
return
def currentproduction(*args):
item = int(self.item.get())
case = int(self.case.get())
currprod = item * case
self.production.set(currprod)
return
def lostunits(*args):
cycle = int(self.cyc.get())
prod = int(self.production.get())
self.loss.set(cycle - prod)
return
def efficiency(*args):
lost = float(self.loss.get())
prod = float(self.production.get())
self.efficiency.set((lost/prod)*100)
return
def getSec(x):
l = x.split(':')
return int(l[0]) * 3600 + int(l[1]) * 60 + int(l[2])
def future_time_seconds(*args):
hrs = self.hour.get()
mins = self.minute.get()
return (int(hrs) * 3600) + (int(mins) * 60)
def time_difference_seconds(*args):
fseconds = future_time_seconds()
s = time.strftime('%I:%M:%S')
cursecs = getSec(s)
return fseconds - cursecs
def proj(*args):
ctime = float(self.cycletime.get())
prod = int(self.production.get())
loss = int(self.loss.get())
case = float(self.case.get())
molds = int(self.vj.get())
item = int(self.item.get())
seconds = time_difference_seconds()
pcycle = ((molds / ctime) * seconds)
projeff = float(self.peff.get()) / float(100)
pproduction = pcycle - (pcycle * projeff)
self.projectedprod.set(prod + pproduction)
projloss = loss + pcycle * projeff
self.ploss.set(projloss)
fcase = case + (pproduction / float(item))
self.fcase.set(fcase)
###line 19
self.ctlabelj = Label(self.line19, text = "Cycle Time:")
self.ctlabelj.grid(row=2, column=0)
self.cycletime = StringVar()
self.cycletime.trace('w', proj)
self.cycletimeentj = Entry(self.line19, textvariable=self.cycletime)
self.cycletimeentj.grid(row=2,column=1)
moldoptionsj = [1, 1, 2, 3, 4]
self.vj = IntVar()
self.vj.set(moldoptionsj[0])
self.headslabelj = Label(self.line19, text = "# of Molds:")
self.headslabelj.grid(row=3, column=0)
self.headcomboj = OptionMenu(self.line19, self.vj, *moldoptionsj)
self.headcomboj.grid(row=3,column=1)
self.vj.trace("w", cyclecnt)
self.cclabelj = Label(self.line19, text = "Cycle Count:")
self.cclabelj.grid(row=4, column=0)
self.cyccnt = StringVar()
self.cyclecountentj = Entry(self.line19, textvariable=self.cyccnt)
self.cyclecountentj.grid(row=4,column=1)
self.cyccnt.trace("w", cyclecnt)
self.ipcj = Label(self.line19, text = "Items/Case:")
self.ipcj.grid(row=5, column=0)
self.item = StringVar()
self.ipcentj = Entry(self.line19, textvariable=self.item)
self.ipcentj.grid(row=5,column=1)
self.item.trace("w", currentproduction)
self.currj = Label(self.line19, text = "Current Case #:")
self.currj.grid(row=6, column=0)
self.case = StringVar()
self.currentj = Entry(self.line19, textvariable=self.case)
self.currentj.grid(row=6,column=1)
self.case.trace("w", currentproduction)
self.ctimej = Label(self.line19, text = "Current Time:")
self.ctimej.grid(row=7, column=0, sticky='W')
self.clockj = Label(self.line19)
self.clockj.grid(row=7,column=1, sticky='w')
####futureztime###
self.futureframe = Frame(self.line19)
self.futureframe.grid(row=8, column=1)
self.futurej = Label(self.line19, text = "Future Projections time:")
self.futurej.grid(row=8, column=0, sticky='w')
self.hour = StringVar()
self.hour.trace('w', time_difference_seconds)
self.hour.trace('w', proj)
self.futureenthourj = Entry(self.futureframe, width=2, textvariable=self.hour)
self.futureenthourj.grid(row=0, column=0)
self.futurecolonj = Label(self.futureframe, text = ":")
self.futurecolonj.grid(row=0, column=1)
self.minute = StringVar()
self.minute.trace('w', time_difference_seconds)
self.minute.trace('w', proj)
self.futureentminj = Entry(self.futureframe, width=2, textvariable=self.minute)
self.futureentminj.grid(row=0, column=2)
####
self.cycleslabel = Label(self.line19, text = 'Cycle Total:')
self.cycleslabel.grid(row=2, column=2)
self.cyc = StringVar()
self.cyc.set("00000")
self.cyc.trace('w', lostunits)
self.cycles = Label(self.line19, foreground = 'green', background = 'black', text = "00000", textvariable = self.cyc)
self.cycles.grid(row=2, column=3)
self.currprodkeylabel = Label(self.line19, text = 'Current Production:')
self.currprodkeylabel.grid(row=3, column=2)
self.production = StringVar()
self.production.set('00000')
self.production.trace('w', lostunits)
self.production.trace('w', efficiency)
self.currentprod = Label(self.line19, foreground = 'green', background = 'black', textvariable=self.production)
self.currentprod.grid(row=3, column=3)
self.prodprojkeylabel = Label(self.line19, text = 'Projected Production:')
self.prodprojkeylabel.grid(row=4, column=2)
self.projectedprod = StringVar()
self.projectedprod.set('00000')
self.prodproj = Label(self.line19, foreground = 'green', background = 'black', textvariable=self.projectedprod )
self.prodproj.grid(row=4, column=3)
self.losskeylabel = Label(self.line19, text = 'Lost units:')
self.losskeylabel.grid(row=5, column=2)
self.loss = StringVar()
self.loss.set("0000")
self.loss.trace('w', efficiency)
self.lossprod = Label(self.line19, foreground = 'green', background = 'black', textvariable=self.loss)
self.lossprod.grid(row=5, column=3)
self.plosskeylabel = Label(self.line19, text = 'Projected Lost units:')
self.plosskeylabel.grid(row=6, column=2)
self.ploss = StringVar()
self.ploss.set("0000")
self.plossprod = Label(self.line19, foreground = 'green', background = 'black', textvariable=self.ploss)
self.plossprod.grid(row=6, column=3)
self.currefficiencykeylabel = Label(self.line19, text = 'Current efficiency %:')
self.currefficiencykeylabel.grid(row=7, column=2)
self.efficiency = StringVar()
self.efficiency.set("00.00")
self.currentefficiency = Label(self.line19, foreground = 'green', background = 'black', textvariable=self.efficiency)
self.currentefficiency.grid(row=7, column=3)
self.futurecaselabel = Label(self.line19, text = 'Future case # projection:')
self.futurecaselabel.grid(row=8, column=2)
self.fcase = StringVar()
self.fcase.set("000.0")
self.futurecase = Label(self.line19, foreground = 'green', background = 'black', textvariable=self.fcase)
self.futurecase.grid(row=8, column=3)
self.projefficiencylabel = Label(self.line19, text = "Efficiency Projection:")
self.projefficiencylabel.grid(row=9, column=2)
self.peff = StringVar()
self.peff.set(0.00)
self.peff.trace('w', proj)
self.projefficiency = Entry(self.line19, textvariable=self.peff)
self.projefficiency.grid(row=9,column=3)
def tick():
s = time.strftime('%I:%M:%S')
if s != self.clockj:
self.clockj.configure(text=s)
self.notebook.after(200, tick)
tick()
root = Tk()
root.wm_title("Hillside Plastics Production Calculator")
calc = supervisorcalc(root)
mainloop()
pythonic解决方案是使每个笔记本选项卡都成为自定义类的实例。它看起来像这样:
class NotebookTab(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.create_widgets()
def create_widgets(self):
self.ctlabelj = Label(self, text = "Cycle Time:")
self.ctlabelj.grid(row=2, column=0)
self.cycletime = StringVar()
self.cycletime.trace('w', proj)
self.cycletimeentj = Entry(self, textvariable=self.cycletime)
self.cycletimeentj.grid(row=2,column=1)
moldoptionsj = [1, 1, 2, 3, 4]
self.vj = IntVar()
...
for linenumber in range(10, 20):
tab = NotebookTab(notebook, text="Line %d" % linenumber)
notebook.add(self)
pythonic解决方案是使每个笔记本选项卡都成为自定义类的实例。它看起来像这样:
class NotebookTab(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.create_widgets()
def create_widgets(self):
self.ctlabelj = Label(self, text = "Cycle Time:")
self.ctlabelj.grid(row=2, column=0)
self.cycletime = StringVar()
self.cycletime.trace('w', proj)
self.cycletimeentj = Entry(self, textvariable=self.cycletime)
self.cycletimeentj.grid(row=2,column=1)
moldoptionsj = [1, 1, 2, 3, 4]
self.vj = IntVar()
...
for linenumber in range(10, 20):
tab = NotebookTab(notebook, text="Line %d" % linenumber)
notebook.add(self)
嗯,使用循环很容易:看(我认为只有一个问题)
嗯,使用循环很容易:看(我认为只有一个问题)
如标签中所述,您是否尝试过使用循环?您可以通过不使用标签的
StringVar
s来保存几行代码。事实上,除非您利用了特殊的StringVar
功能(如跟踪)或将两个或多个小部件的显示绑定在一起,否则您真的不需要将StringVar
用于标签或条目小部件,如标签中所述,您可以通过不使用标签的StringVar
s来保存几行代码。事实上,除非您利用了特殊的StringVar
功能,例如跟踪,否则您真的不需要为标签或条目小部件使用StringVar
s,或者将两个或多个小部件的显示绑定在一起。我现在的问题是,为什么在切换选项卡时,每个选项卡的条目小部件都会保留其输入值,即使它们具有相同的变量名?但是没有一个计算数学的函数在它们上面工作?我是否需要为每个小部件创建一个字典,并为每个函数创建一个循环?赫密斯特里斯密斯特斯:我想是的。您必须为所有面板创建变量。制作字典可能是一个解决办法!你怎么能有9个条目
,而不让9个变量看到它们?您所有的StringVar
可能都在字典中,面板之间的所有差异都必须依赖于面板(即:拥有自己的变量)!不有些人不喜欢使用StringVar
作为条目,而是使用get
和set
,但这里有一件事是一样的:你必须制作一本字典才能访问所有这些条目!是的,我现在的问题是,为什么在切换选项卡时,每个选项卡的条目小部件都会保留它们的输入值,即使它们具有相同的变量名?但是没有一个计算数学的函数在它们上面工作?我是否需要为每个小部件创建一个字典,并为每个函数创建一个循环?赫密斯特里斯密斯特斯:我想是的。您必须为所有面板创建变量。制作字典可能是一个解决办法!你怎么能有9个条目
,而不让9个变量看到它们?您所有的StringVar
可能都在字典中,面板之间的所有差异都必须依赖于面板(即:拥有自己的变量)!不有些人不喜欢使用StringVar
作为条目,而是使用get
和set
,但这里有一件事是一样的:你必须制作一本字典才能访问所有这些条目!对