创建Tkinter按钮以停止/跳过2D循环 [英] Creating Tkinter buttons to stop/skip a 2D loop

查看:42
本文介绍了创建Tkinter按钮以停止/跳过2D循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个tkinter GUI程序,该程序具有读取2D数组中的每个单个元素的功能.我需要具有以下功能的三个按钮(开始",跳过"和停止"):

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:

开始"按钮,程序可以一一读取并打印数组中的元素.例如,在下面的代码中,它首先打印"11",然后打印"12",然后打印"13",然后打印"14",然后打印"21",依此类推,直到完成为止.整个数组.

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.

跳过"按钮允许程序跳过程序正在读取的行.例如,当程序正在打印"12"时,它将跳到第二行并开始打印"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.

停止"按钮停止并显示整个程序.

The "Stop" button stops and whole program.

当前,我可以按照以下示例管理一维循环情况:[TKinter-如何使用停止按钮停止循环?] [1].但是,二维循环仍然是一个巨大的挑战.有没有人有经验?非常感谢!

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()

推荐答案

类似的方法可能对您有用

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()

我使用 x y 跟踪两个维度的数组索引.当我尝试打印下一个项目时,如果我们已经到达当前数组的末尾,将抛出IndexError异常,然后我们将处理该异常以递增到下一行.当我们达到44岁时,我们位于列表的末尾,并且 running 设置为false.

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.

这篇关于创建Tkinter按钮以停止/跳过2D循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆