问题是它只适用于一个全局函数..如何为不同的函数分配每个按钮
from tkinter import*
class mainWindow:
def __init__(self,master):
mainFrame=Frame(master)
mainFrame.pack()
self.cv=Canvas(scrollregion=(0,0,200,1200),width=200,height=1200,bg='green')
self.scb=Scrollbar(command=self.cv.yview)
self.cv.pack(side=LEFT)
self.scb.pack(side=RIGHT,fill=Y)
self.cv.configure(yscrollcommand=self.scb.set)
#frame on canvas to pack title frame
self.cvFrame=Frame(self.cv)
self.cvFrame_window=self.cv.create_window(5,5,anchor=NW,window=self.cvFrame)
self.tFrame=Frame(self.cvFrame)
self.tFrame.pack(in_=self.cvFrame)
self.t=['Site Preparation','WBLFF','Frame','Roof Construction','Roof Finishes',]
self.tr=[0,1,2,3,4,5]
#range button created inside one frame
for i in range(5):
self.tButton=Button(self.tFrame,text=self.t[i],bg='purple',fg='white',width=20,relief=GROOVE)
self.tButton.grid(in_=self.tFrame,row=self.tr[i])
for i in range(0,1):
self.tButton.bind('<Button-1>',self.subT1)
#open up new subtitle under main title
def subT1(self,event):
self.s=['Site Preparation','Site Clearence','Earth Works']
self.sc=['dark violet','yellow','yellow']
self.sf=['white','black','black']
self.sr=[0,1,2]
self.sFrame=Frame(self.tFrame)
self.sFrame.grid(in_=self.tFrame,row=0)
for x in range(3):
self.sBtn=Button(self.sFrame,text=self.s[x],bg=self.sc[x],fg=self.sf[x],width=20,relief=GROOVE)
self.sBtn.grid(in_=self.sFrame,row=self.sr[x])
self.sBtn.bind('<Button-1>',self.destF)
#detroy frame back to main title
def destF(self,event):
self.sFrame.destroy()
#root window
root=Tk()
root.title('Built Up Rates')
root.geometry('220x600+0+0')
A=mainWindow(root)
root.mainloop()
Open Up The Program Open up the subtitle by clicking the button
问题是它只适用于一个全局函数..如何为不同的函数分配每个按钮
答案 0 :(得分:0)
首先:您可以使用按钮(command = function_name)
代替绑定(&#39;&lt; Button-1&gt;&#39;,function_name)
创建功能列表
functions = [self.subT1, self.other_function]
for i in range(2):
Button(command=functions[i])
def subT1(self): # without `event` or with `event=None`
答案 1 :(得分:0)
from tkinter import*
class mainWindow:
def __init__(self,master):
mainFrame=Frame(master)
mainFrame.pack()
self.cv=Canvas(scrollregion=(0,0,200,1200),width=200,height=1200,bg='green')
self.scb=Scrollbar(command=self.cv.yview)
self.cv.pack(side=LEFT)
self.scb.pack(side=RIGHT,fill=Y)
self.cv.configure(yscrollcommand=self.scb.set)
#Frame to Canvas
self.cvFrame=Frame(master)
self.cvFrame_window=self.cv.create_window(5,5,anchor=NW,window=self.cvFrame)
self.t=['Site Preparation','WBLFF','Frame','Roof Construction','Roof Finishes',]
self.tr=[0,1,2,3,4,5]
#range button created inside one frame
for i in range(5):
self.tButton=Button(self.cvFrame,text=self.t[i],bg='purple',fg='white',width=20,relief=GROOVE)
self.tButton.grid(in_=self.cvFrame,row=self.tr[i])
if i in range(0,1):
self.tButton.bind('<Button-1>',self.subT1)
if i in range(1,2):
self.tButton.bind('<Button-1>',self.subT2)
def subT1(self,event):
self.s=['Site Preparation','Site Clearence','Earth Works']
self.sc=['dark violet','yellow','yellow']
self.sf=['white','black','black']
self.sFrame=Frame(self.cvFrame)
self.sFrame.grid(in_=self.cvFrame,row=0)
for x in range(3):
self.sBtn=Button(self.sFrame,text=self.s[x],bg=self.sc[x],fg=self.sf[x],width=20,relief=GROOVE)
self.sBtn.grid(in_=self.sFrame,row=x)
self.sBtn.bind('<Button-1>',self.destF)
def subT2(self,event):
self.s=['WBLFF','Pile Cap','Column Stump','Ground Beam','Ground Slab']
self.sc=['dark violet','yellow','yellow','yellow','yellow']
self.sf=['white','black','black','black','black']
self.sFrame=Frame(self.cvFrame)
self.sFrame.grid(in_=self.cvFrame,row=1)
for x in range(5):
self.sBtn=Button(self.sFrame,text=self.s[x],bg=self.sc[x],fg=self.sf[x],width=20,relief=GROOVE)
self.sBtn.grid(in_=self.sFrame,row=x)
self.sBtn.bind('<Button-1>',self.destF)
#detroy frame back to main title
def destF(self,event):
self.sFrame.destroy()
#root window
root=Tk()
root.title('Built Up Rates')
root.geometry('220x600+0+0')
A=mainWindow(root)
root.mainloop()
得到它:)通过使用'if'语句...在ranged对象中运行良好并分配参数是否有任何其他方法可以使它更简单,如继承或全局定义?