Home > OS >  How to stop tkinter script until button is pressed
How to stop tkinter script until button is pressed

Time:08-04

I am trying to make a tkinter script that won't cycle to the next text until a button is pressed

import tkinter as tk
from tkinter import *
window = tk.Tk()
window.geometry("300x300")

TabNum = ["1","2","3"]
TabNumInt = 0
Text = tk.Label(text = ("Page number"   TabNum[TabNumInt]))
Text.pack()
def continuefunct():
  global Running
  TabNumInt =  1
Continuebtn = Button(window, text = 'Continue', bd = '5', command = continuefunct()).pack()

tk.mainloop()

I want this to "print page number [1]" and for the number to increase everytime you press the button until you get to 3. (I know I could have while while TabNumInt =< len(TabNum) but this is just a proof of concept). Any help would be appreciated :)

CodePudding user response:

There are few issues in your code:

  • command = continuefunct() will execute the function immediately without clicking the button. Also nothing will be performed when the button is clicked later.
  • need to declare TabNumInt as global variable inside the function
  • TabNumInt = 1 should be TabNumInt = 1 instead
  • need to update text of the label inside the function

Below is the modified code:

import tkinter as tk

window = tk.Tk()
window.geometry("300x300")

TabNum = ["1", "2", "3"]
TabNumInt = 0

Text = tk.Label(window, text="Page number " TabNum[TabNumInt])
Text.pack()

def continuefunct():
    global TabNumInt
    if TabNumInt < len(TabNum)-1:
        TabNumInt  = 1
        Text.config(text="Page number " TabNum[TabNumInt])

tk.Button(window, text='Continue', bd='5', command=continuefunct).pack()

window.mainloop()
  • Related