0

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)
matszwecja
  • 6,357
  • 2
  • 10
  • 17
kamcho 56
  • 25
  • 3
  • First of all, you shouldn't be using `time.sleep` when using `tkinter`. Second of all, `tkinter` has a way of getting the mouse position so `..., (x, y) = win32gui.GetCursorInfo()` can be replaced with `x, y = win.winfo_pointerx(), win.winfo_pointery()`. Also please give us a [mre]. Right now there is too much code. – TheLizzard Sep 16 '22 at 11:28
  • i'd say thgere is too little code regarding used modules. What do you import and how exactly – Psytho Sep 16 '22 at 11:30
  • why do you mix `tkinter` and `wx` ? This can makes problem. Maybe `wx.App` needs Enter to exit program. `tkinter` has own methods to get display size. – furas Sep 16 '22 at 11:36
  • BTW: `Tk()` should be used only to create (one) main window. If you need many widows at the same time then you should use `Toplevel()` – furas Sep 16 '22 at 11:38
  • [python - How can I get the screen size in Tkinter? - Stack Overflow](https://stackoverflow.com/questions/3949844/how-can-i-get-the-screen-size-in-tkinter) – furas Sep 16 '22 at 11:40
  • @TheLizzard Thank you for replying. I will look into an alternative to time.sleep() for tkinter and thank you for the alternative to geting cursor info. As for the reproducible example, im not sure what you mean. Are you asking me to remove all code that i dont think is related to the problem? if so i can do that, but i didnt want to remove something which i though wasnt the issue only for it to turn out i had removed something important. Shall i reduce the code? – kamcho 56 Sep 16 '22 at 11:40
  • @kamcho56 Remove parts of the code that aren't responsible for the issue but make sure it can still run. It's easier to work with a minimal example that we can copy/paste/run. – TheLizzard Sep 16 '22 at 11:44
  • @furas your solution was correct, after replacing wx.App resolution code with the tkinter variation the code no longer requires an enter key press. Thank you everyone who helped. Would you like to post a solution and i can accept it as "correct solution"? – kamcho 56 Sep 16 '22 at 12:37
  • @TheLizzard Thanks for your help, The problem has been solved but i will remember your tips when asking future questions. – kamcho 56 Sep 16 '22 at 12:40
  • OK, another update. Turns out i changed the code to detect the resolution and removed the time.sleep() function in the same test. and to my surprise it appears to be the time.sleep function which is causing this. After removing the time.sleep() function the code doesnt require the enter key. – kamcho 56 Sep 16 '22 at 13:01
  • you could describe all your solutions in answer below and later you can accept your asnwer. – furas Sep 16 '22 at 13:50

1 Answers1

0

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.

kamcho 56
  • 25
  • 3