我正在尝试创建一个从列表生成按钮的系统,然后按下每个按钮时该按钮消失并更新按钮按下的顺序创建的列表中的值,即[ 1,3,2,4]意味着先按下第一个按钮,然后按下第3个,第2个按钮等。
我现在已经在按钮消失但我还不能增加订单或生成列表的阶段。
def buttonSelected(k, buttonNames, order, itemButton, buttonOrder):
order = order + 1
itemButton[k].destroy()
buttonOrder[k] = order
orderText = ["first, second, third"]
if order <= 2:
lbl.configure(text="Which button do you want to click first?")
else:
lbl.configure(text="Which button do you want to click %sth?" % order)
return order
def chooseOrder(buttonNames, order, buttonOrder):
orderText = ["first, second, third"]
itemButton = []
for i in range(len(buttonNames)):
print i
itemButton.append('')
itemButton[i] = tkinter.Button(window, text=buttonNames[i], command=lambda c=i: buttonSelected(c, buttonNames, order, itemButton, buttonOrder), font=("Helvetica", 10))
itemButton[i].pack()
if order <= 2:
lbl.configure(text="Which button do you want to click first?")
else:
lbl.configure(text="Which button do you want to click %sth?" % order)
buttonOrder = []
for i in range(len(buttonNames)):
buttonOrder.append(0)
chooseOrder(buttonNames, 0, buttonOrder)
我的订单没有迭代,因为我没有返回它,我也不知道如何返回列表。
这是我的完整代码:
# import and rename the 'tkinter' module for < Python 3.3
import Tkinter as tkinter
import os
class Checkbar(tkinter.Frame):
def __init__(self, parent=None, picks=[], side=tkinter.TOP, anchor=tkinter.W):
tkinter.Frame.__init__(self, parent)
self.vars = []
for pick in picks:
var = tkinter.IntVar()
chk = tkinter.Checkbutton(self, text=pick, variable=var, font=("Helvetica", 10))
chk.pack(side=side, anchor=anchor, expand=tkinter.YES)
self.vars.append(var)
def state(self):
return map((lambda var: var.get()), self.vars)
def moduleSelected(k, wantedModules, order, itemButton, moduleOrder):
order = order + 1
#for j in range(len(itemButton)):
itemButton[k].destroy()
moduleOrder[k] = order
#chooseOrder(wantedModules, order)
orderText = ["first, second, third"]
if order <= 2:
lbl.configure(text="Which item do you want appearing first?")
else:
lbl.configure(text="Which item do you want appearing %sth?" % order)
if order == len(moduleOrder):
moduleOrder1Finished = 1
return order
def chooseOrder(wantedModules, order, moduleOrder):
orderText = ["first, second, third"]
itemButton = []
for i in range(len(wantedModules)):
print i
itemButton.append('')
itemButton[i] = tkinter.Button(window, text=wantedModules[i], command=lambda c=i: moduleSelected(c, wantedModules, order, itemButton, moduleOrder), font=("Helvetica", 10))
itemButton[i].pack()
print wantedModules
if order <= 2:
lbl.configure(text="Which button do you want appearing first?")
else:
lbl.configure(text="Which button do you want appearing %sth?" % order)
def chooseOrders(moduleList, moduleSelectColumn, order):
wantedModules = []
moduleOrder = []
for i in range(len(moduleList)):
if moduleList[i] == 1:
wantedModules.append(moduleSelectColumn[i])
moduleOrder.append(0)
chooseOrder(wantedModules, order, moduleOrder)
def modulesSelected(modules1, moduleSelectColumn1, modules2, moduleSelectColumn2, moduleSelectButton):
moduleList1 = modules1.state()
moduleList2 = modules2.state()
modules1.destroy()
modules2.destroy()
moduleSelectButton.destroy()
moduleOrder1 = chooseOrders(moduleList1, moduleSelectColumn1, 0)
moduleOrder2 = chooseOrders(moduleList2, moduleSelectColumn2, 0)
def produceSurvey():
moduleSelectColumn = []
for i in range(4):
moduleSelectColumn.append([])
moduleSelectColumn[1] = ['cheese', 'wine', 'bread', 'cereal']
moduleSelectButton = tkinter.Button(window, text="Finish Selecting", bg="#ffffff", font=("Helvetica", 16), command=lambda: modulesSelected(modules1, moduleSelectColumn[1], modules2,moduleSelectColumn[2], moduleSelectButton))
moduleSelectButton.pack(side=tkinter.TOP)
modules1 = Checkbar(window, moduleSelectColumn[1])
modules2 = Checkbar(window, moduleSelectColumn[2])
modules1.pack(side=tkinter.LEFT)
modules2.pack(side=tkinter.RIGHT)
lbl.configure(text="Select which buttons you want to Select?")
window.geometry("720x720")
def analSurvey():
print "analysing survey!"
def optionButton(option, option1, option2):
option1.destroy()
option2.destroy()
if option == 1:
produceSurvey()
elif option == 2:
analSurvey()
def callback_and_hide(button):
callback()
button.destroy()
def callback():
lbl.configure(text="What would you like to do?")
option1 = tkinter.Button(window, text="Help JameswDemps on stackexchange", bg="#ffffff", font=("Helvetica", 16), command=lambda: optionButton(1, option1, option2))
option1.pack()
option2 = tkinter.Button(window, text="go to a blank page", bg="#ffffff", font=("Helvetica", 16), command=lambda: optionButton(2, option1, option2))
option2.pack()
# create a new window
window = tkinter.Tk()
# set window title
window.title("Button Presser")
# Gets the requested values of the height and widht.
windowWidth = window.winfo_reqwidth()
windowHeight = window.winfo_reqheight()
# Gets both half the screen width/height and window width/height
positionRight = int(window.winfo_screenwidth()/2 - windowWidth/2)
positionDown = int(window.winfo_screenheight()/2 - windowHeight/2)
# Positions the window in the center of the page.
window.geometry("+{}+{}".format(positionRight-250, positionDown-150))
window.geometry("720x480")
# set the window background to hex code ...
window.configure(background="#ffffff")
# create a label widget call 'lbl'
lbl = tkinter.Label(window, text="Welcome to the Button presser!", bg="#ffffff", font=("Helvetica", 24))
# creat a text entry widget called 'ent'
#ent = tkinter.Entry(window)
# create a button widget called btn
startBtn = tkinter.Button(window, text="Start", command=lambda: callback_and_hide(startBtn), font=("Helvetica", 16))
# pack (add) the widget into the window
lbl.pack()
#ent.pack()
startBtn.pack()
# draw the window, and start the 'application'
window.mainloop()
答案 0 :(得分:0)
RESULT = []
def moduleSelected(k, wantedModules, order, itemButton, moduleOrder):
order = order + 1
#for j in range(len(itemButton)):
itemButton[k].destroy()
moduleOrder[k] = order
print k
RESULT.append(k+1)
print RESULT # -> This will print the output you want.
#chooseOrder(wantedModules, order)
orderText = ["first, second, third"]
if order <= 2:
lbl.configure(text="Which item do you want appearing first?")
else:
lbl.configure(text="Which item do you want appearing %sth?" % order)
if order == len(moduleOrder):
moduleOrder1Finished = 1
return order
我添加了一个RESULT列表来存储点击它们的按钮的顺序。