Home > Blockchain >  How I can stop the printer function?
How I can stop the printer function?

Time:10-18

    from tkinter import *
    
    root = Tk()
    
    #This function should stop the function printer
    def button_status(is_clicked):
        return is_clicked
    
    #This function prints integer numbers from 0 to 2
    def printer():
        for i in range(3):
            print(i)
            if(button_status==True):
                break
                
    button = Button(root, text="Stampa",height=2, width=6, command= printer, font=("TkDefaultFont",18),bg='white')
    button.place(x=630, y=680)    
    button3 = Button(root, text="Termina",height=2, width=6, command= lambda: button_status(True), font=("TkDefaultFont",18),bg='white')
    button3.place(x=780, y=680)
    
    print(button_status)
    root.state('zoomed')
    root.mainloop()

CodePudding user response:

There are a things you'll need to change in your code to allow one function to control the behavior of the other.

The first thing that needs to change is that printer needs not to be using a Python loop. If it does that, tkinter won't have any opportunity to run any other code, since it will be waiting for the printer function to return first. Instead, make the button that calls printer the first time pass in a number and have the function schedule itself to be called again by the tkinter main loop after a short delay.

def printer(i):
    print(i)                            # Note, this always prints at least once. Move the
    if i < 2 and not button_status:     # print call inside the `if` if you don't want that.
        root.after(1000, printer, i 1)  # Schedule ourselves to be called again in one second

button = Button(root, text="Stampa",height=2, width=6, command=lambda: printer(0),
                font=("TkDefaultFont",18),bg='white')
button.place(x=630, y=680)    

The other thing you need to change is some way of sharing state between the two functions. The easiest way to do that would be with a global variable, but you might consider redesigning your two functions as methods of a class instead, so that they can share state via instance variables instead. Here's what the global variable setup would look like (printer above will already work with this code):

button_status = False     # Set an initial value

def stop_counting():
    global button_status  # This tells Python we want to be able to change the global variable
    button_status = True  # Use `button_status = not button_status` if you want it to toggle

button3 = Button(root, text="Termina",height=2, width=6, command=stop_counting,
                 font=("TkDefaultFont",18),bg='white')
button3.place(x=780, y=680)

CodePudding user response:

Another way to stop a printing output is to use a flag boolean.

This method stores the flag in a list, which prevents the need for global statements in functions.

import tkinter as tk

root = tk.Tk()

# termination flag in list
termina = [ False ]

#This function should stop the function printer
def button_status():
    termina[ 0 ] = True

#This function prints integer numbers from 0 to 2999
def printer():
    # Setup termination flag for printing
    termina[ 0 ] = False
    for i in range(3000):
        root.update()
        if termina[ 0 ]: # test for True status
            break
        else:
            print(i)
            
buttonA = tk.Button(
    root, text="Stampa", command= printer,
    font=("TkDefaultFont",18), bg='white')
buttonA.grid(sticky = tk.NSEW)

buttonB = tk.Button(
    root, text="Termina", command= button_status,
    font=("TkDefaultFont",18), bg='white')
buttonB.grid(sticky = tk.NSEW)

# root.state('zoomed')
root.mainloop()
  • Related