Home > Software engineering >  Why does my program require me to press the enter key before continuing (python tkinter)
Why does my program require me to press the enter key before continuing (python tkinter)

Time:09-24

I am making a program, using python tkinter, which simply prints some circles to the screen (I call it a board in this program). The program moves on to a different "board" once the mouse cursor moves over the button. The problem I have is that I simply call the "create_board" function 3 times using a for loop however in-between each iteration of the loop the "enter" key must be pressed. This isn't a massive deal but I'm trying to understand why and if there is a way to remove this requirement and have the next board load automatically.

I'm certain it has something to do with the tkinter windows and triggering the command "destroy" once the buttons (circles) are pressed however I'm still learning how to effectively use tkinter and any help would be very much appreciated.

def create_board(user_name, board):

    # define the name of tkinter window
    win = Tk()

     # get the size of the displace for position and size calculations
    app = wx.App(False)
    w, h = wx.GetDisplaySize()
 
    name = user_name

    # define variables based on board number
    if board == 0:
        gx_pos = int(w/8) # locations of circles
        gy_pos = int(h/8)
        bx_pos = (w/8)*5
        by_pos = (h/8)*5
        board_num = str(1)
    elif board == 1:
        gx_pos = int(w/12)
        gy_pos = int(h/12)
        bx_pos = (w/6)*5
        by_pos = (h/6)*5
        board_num = str(2)
    elif board == 2:
        gx_pos = int(w/3)
        gy_pos = int(h/3)
        bx_pos = (w/3)*2
        by_pos = (h/3)*2
        board_num = str(3)

    # records the mouse cursor position into a file along with time taken
    def record_pos(x, y, board_num, s):
        filename = name   "_"   board_num   ".txt"
        try:
            os.path.isfile('./' filename)
        except:
            open(filename, 'r')

        with open(filename, 'a') as f:
            f.write(str(x)   ","   str(y)   ","   str(s)   "\n")
    
    # determining when left click should be made
    def mouse_pos():
        flags, hcursor, (x, y) = win32gui.GetCursorInfo()
        time_taken = time.time()
        record_pos(x, y, board_num, time_taken)
        mouse.click('left')
        win.after(500, mouse_pos)
    
    # wait 3 seconds before loading first board
    time.sleep(3)
    geometry = "%dx%d" % (w,h)
    win.geometry(geometry)
    win.attributes('-fullscreen', True)
    win.config(cursor="circle")

    # get the grid image
    bg = Image.open("grid_image.png")
    img = bg.resize((w, h))
    grid_img=ImageTk.PhotoImage(img)
    image_label = Label(win, image=grid_img)
    image_label.place(x=0, y=0, relwidth=1, relheight=1)
    
    # print an image of a green circle
    gw = int(w/26)
    gh = int(h/15)
    g_circle = Image.open('green_circle.png')
    g_img = g_circle.resize((gw,gh))
    g_circle_image=ImageTk.PhotoImage(g_img)
    g_label = Label(win, image=g_circle_image)
    g_label.place(x = gx_pos,y = gy_pos)
    g_btn = Button(win, image=g_circle_image, command = win.destroy)
    g_btn.place(x= gx_pos , y= gy_pos)

    # print an image of a blue circle
    bw = int(w/26)
    bh = int(h/15)
    b_circle = Image.open('circle.png')
    b_img = b_circle.resize((bw,bh))
    b_circle_image=ImageTk.PhotoImage(b_img)
    b_label = Label(win, image=b_circle_image)
    b_label.place(x=bx_pos, y=by_pos)
    b_btn = Button(win, image=b_circle_image, command = win.destroy)
    b_btn.place(x=bx_pos, y=by_pos)

    # record mouse position 
    mouse_pos()
    win.mainloop()

EDIT: I added the simple for loop that I'm using to iterate through the boards.

for i in range(3):
    create_board(user_name, i)

CodePudding user response:

The issue was caused by using time.sleep() in tkinter. After removing this the code runs with out requiring an enter key press each time.

  • Related