【发布时间】:2021-01-07 13:39:50
【问题描述】:
我正在开发一个 tkinter GUI 程序,它具有读取二维数组中每个元素的功能。我需要三个按钮(“开始”、“跳过”和“停止”),它们具有以下功能:
“开始”按钮让程序一个一个地读取和打印数组中的元素。例如,在下面的代码中,它首先打印“11”,然后是“12”,然后是“13”,然后是“14”,然后是“21”,以此类推,直到完成整个数组。
“跳过”按钮允许程序跳过程序正在读取的行。例如,当程序打印“12”时,如果我点击“跳过”按钮,它将跳转到第二行并开始打印“21”。
“停止”按钮停止,整个程序。
当前,我可以按照以下示例管理一维循环案例:[TKinter - 如何使用停止按钮停止循环?][1]。然而,二维循环仍然是一个巨大的挑战。有没有人有经验?非常感谢!
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()
【问题讨论】: