Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
332 views
in Technique[技术] by (71.8m points)

python - Creating Tkinter buttons to stop/skip a 2D loop [Solved]

I am developing a tkinter GUI program which has the function of reading each single element in a 2D array. I need three buttons ("Start", "Skip" and "Stop") which have the following functions:

The "Start" button let the program read and print the element inside the array one by one. For example, in the following code, it first print "11", and then "12", and then "13", and then "14", and then "21", and so on until it finishes the whole array.

The "Skip" button allows the program to skip the row which is being read by the program. For example, when the program is printing "12", it will jump to the second row and start to print "21" if I click the "Skip" button.

The "Stop" button stops and whole program.

Current, I can manage the 1D loop case following this example: [TKinter - How to stop a loop with a stop button?][1]. However, a 2D loop is still a huge challenge. Does anyone has the experience? Many thanks!

import tkinter as tk
import time

#Initialize the Root
root= tk.Tk()
root.title("Real time print")
root.configure(background = "light blue")
root.geometry("500x420")
 
# The array to be printed
array = [[11,12,13,14],[21,22,23,24],[31,32,33,34],[41,42,43,44]]
 
# Define the function to print each element in the array
def do_print():
    print("The element inside the array")
    time.sleep(1)
    return
 
#Define strat, skip and stop buttons
def start_button():
    do_print()
    return
 
start = tk.Button(root, text = "Start", font = ("calbiri",12),command = start_button)
start.place(x = 100, y=380)
 
def skip_button():
    return
skip = tk.Button(root, text = "Skip", font = ("calbiri",12),command = skip_button)
skip.place(x = 160, y=380)
 
def stop_button():
    return
 
stop = tk.Button(root, text = "Stop", font = ("calbiri",12),command = stop_button)
stop.place(x = 220, y=380)
 
root.mainloop()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Something like this might work for you

import tkinter as tk

x = 0
y = 0
array = [[11,12,13,14],[21,22,23,24],[31,32,33,34],[41,42,43,44]]
running = True

def start():
    global x, y, running
    x = y = 0
    running = True
    output()

def skip():
    global x, y
    x +=1
    y = 0

def stop():
    global running
    running = False

def output():
    global x, y, running
    try:
        print(array[x][y])
    except IndexError as e:
        if x >= len(array):
            #Run out of lists
            running = False
        elif y >= len(array[x]):
            y = 0
            x += 1
        else:
            raise e
    y += 1
    if running:
        root.after(1000, output)

root = tk.Tk()
btnStart = tk.Button(root,text="Start",command=start)
btnSkip = tk.Button(root,text="Skip",command=skip)
btnStop = tk.Button(root,text="Stop",command=stop)

btnStart.grid()
btnSkip.grid()
btnStop.grid()

root.mainloop()

I'm keeping track of the index in to the array for both dimensions using x and y. When I try to print the next item, if we've reached the end of the current array, the IndexError exception will be thrown which we then handle to increment on to the next row. When we reach 44, we are at the end of the list and running is set to false.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...