简体   繁体   English

创建 Tkinter 按钮以停止/跳过 2D 循环

[英]Creating Tkinter buttons to stop/skip a 2D loop

I am developing a tkinter GUI program which has the function of reading each single element in a 2D array.我正在开发一个 tkinter GUI 程序,它具有读取二维数组中每个单个元素的 function。 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.例如,在下面的代码中,它首先打印“11”,然后是“12”,然后是“13”,然后是“14”,然后是“21”,依此类推,直到完成整个数组。

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.例如,当程序正在打印“12”时,如果我单击“跳过”按钮,它将跳转到第二行并开始打印“21”。

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].目前,我可以按照以下示例管理一维循环案例:[TKinter - 如何使用停止按钮停止循环?][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()

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 .我正在使用xy跟踪两个维度的数组索引。 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.当我尝试打印下一项时,如果我们已经到达当前数组的末尾,则会抛出 IndexError 异常,然后我们会处理该异常以递增到下一行。 When we reach 44, we are at the end of the list and running is set to false.当我们达到 44 时,我们位于列表的末尾,并且running设置为 false。

You could use a Thread.你可以使用一个线程。

import threading

array = [1,2,3,4,5,6]
running == False

def do_print():
    count = 0
    while running == True:
        print(array[count])
        count += 1
def stop():
    running = False

x = threading.Thread(target=do_print)
y = threading.Thread(target=stop)

btnStart = tk.Button(root,text="Start",command=x.start)
btnStop = tk.Button(root,text="Stop",command=y.start)
       
 

This is just an exemple with a 1 dimesion array.这只是一个 1 维数组的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM