Home>

Display the image on tkinter → Evaluate whether you like or dislike → Evaluate whether you like or dislike with the new image
I want to repeat the trial a certain number of times, and I am trying to loop the whole program with for, but even if it exceeds that number, it does not end in one direction and I can press the button indefinitely.
I thought that it would be better to loop btn.bind in order to turn the operation of pressing the button a fixed number of times, but there was no such example even after investigating, and I could not solve it and asked a question. It was
I would appreciate it if anyone could teach me.

Applicable source code
root = tkinter.Tk()
root.title("title")
for i range (10):
    def func1(event):
        # It is a program that presents various images
..
..
..
    def culc1(event):
        print (I like)
        func1(event):
    def culc2(event):
        print (dislike)
Func1(event):
    btn1= tkinter.Button(root, text='like', width=10)
    btn1.bind("<Button-1>",culc1)
    btn1.place(x=150,y=200)
    btn2= tkinter.Button(root, text='I hate', width=10)
    btn2.bind("<Button-1>",culc2)
    btn2.place(x=200,y=200)
tkinter.mainloop()
Supplemental information (FW/tool ​​version, etc.)

python3

  • Answer # 1

    The code of the subject is not limited to the event many times with the for statement
    Every time I create a widget and define a function, it becomes a code such as.

    Prepare a variable to measure the number of clicks and a variable to set the upper limit
    How about a code that branches the process when the count reaches the upper limit?

    import tkinter
    root = tkinter.Tk()
    root.title("title")
    limit = 10
    cnt = 0
    def func1(event):
        # It is a program that presents various images
    def culc1(event):
        global cnt
        cnt += 1
        if cnt<= limit:
            func1(event)
        else:
            limit_over()
    def culc2(event):
        global cnt
        cnt += 1
        if cnt<= limit:
            func1(event)
        else:
            limit_over()
    def limit_over():
        # Process when cnt reaches limit (0 is reset to cnt and reset, button is set to state=disabled, etc.)
    btn1= tkinter.Button(root, text='like', width=10)
    btn1.bind("<Button-1>",culc1)
    btn1.place(x=150,y=200)
    btn2= tkinter.Button(root, text='I hate', width=10)
    btn2.bind("<Button-1>",culc2)
    btn2.place(x=200,y=200)
    tkinter.mainloop()