【问题标题】:Creating Tkinter buttons to stop/skip a 2D loop创建 Tkinter 按钮以停止/跳过 2D 循环
【发布时间】: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()

【问题讨论】:

    标签: python tkinter button


    【解决方案1】:

    这样的东西可能对你有用

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

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

    【讨论】:

      【解决方案2】:

      你可以使用线程。

      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)
             
       
      

      这只是一个一维数组的例子。

      【讨论】:

      • 谢谢!我会尝试看看是否可以在二维数组中使用相同的技术。
      • 如果有效,请告诉我,我没有错。好好学习!
      猜你喜欢
      • 2017-01-26
      • 2022-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-18
      • 2014-01-21
      • 1970-01-01
      • 2021-03-11
      相关资源
      最近更新 更多