我想知道如何在Tkinter中点击按钮一定次数时停止一个功能。我的项目是一个测验程序,当您单击开始按钮时,第一个问题从文本文件打印到TextBox中。然后,当您选择多项选择答案,然后单击提交按钮时,下一个随机选择的问题将替换文本框中的上一个问题,并且每次单击提交< / em>按钮。
我想知道如何启动鼠标点击计数器,并在一定次数的点击后停止在文本框中打印行。
因此,当单击开始按钮时,计数器将转到1,然后当单击提交按钮时,相同的计数器将增加1次单击每次点击。
然后,点击开始按钮1次后,点击提交 9次,点击总数将等于10,打印行会停下来。
以下是我对代码的尝试:
def Maths():
Maths = Tk()
Maths.title("Student Mode")
Maths.geometry("1200x600")
lines = open('AnswersMaths.txt').read().splitlines()
count1 = 1 #I set this as a counter
def start_random():
myline =random.choice(lines)
Question_Ans.insert(END, myline)
count1 + 1 #When the start button is clicked, the button is added by 1
Question_Ans = Text(Maths, width = 50, height = 20)
Question_Ans.place(x=40, y=100)
Start_Button = Button(Maths, width = 10, text = "Start", command=start_random)
Start_Button.place(x=500, y=110)
var = StringVar(Maths)
var.set(None)
option = OptionMenu(Maths, var,"A", "B", "C", "D", "E")
option.place(x=500, y=160)
c = open("AnswersMaths.txt", 'r+')
def AnswerCode():
c.write(var.get() + '\n')
print var.get()
myline =random.choice(lines)
Question_Ans.delete('1.0', END)
Question_Ans.insert(END, myline)
count1 + 1 #When the submit button is clicked, the button is also added by 1
if count1 > 10: #When the counter is > 10, print 'finish'.
print 'Finish'
Drop_button = Button(Maths,width = 10, text="Submit", command=AnswerCode)
Drop_button.place(x=500, y=200)
def delete_line():
with open('AnswersMaths.txt', 'r') as fin:
data = fin.read().splitlines(True)
with open('AnswersMaths.txt', 'w') as fout:
fout.writelines(data[1:])
line_delbutton = Button(Maths,width = 10, text="Finish", command=delete_line).place(x=500, y=240)
exit1_button=Button(Maths,width = 5, text="Exit", command=Maths.destroy).place(x=1100, y=550)
Inst_Button1=Button(Maths, text="Instructions", command=subj_Instructions).place(x=10, y=10)
Label_maths = Label(Maths, width = 10, font=("Adobe Gothic Std B",30), text="Maths Quiz").place(x=500, y=10)
在我的代码中,我尝试将变量 count1 设置为计数器,因此当单击开始按钮时,将添加 count1 1,当单击提交按钮时,它也会被添加1,直到 count1 &lt; 10,打印单词 Finsh 。不幸的是,这不起作用。
希望我很清楚,仍然是python的新手,所以代码的任何细节都会受到赞赏。谢谢你的帮助