简体   繁体   English

用按钮停止程序 Tkinter Python 3

[英]Stopping program with button Tkinter Python 3

I have a program that works with an user interface : I start it using a button and I would like to have the possibility to stop it during the process using the user interface:我有一个与用户界面配合使用的程序:我使用按钮启动它,我希望有可能在使用用户界面的过程中停止它:

在此处输入图片说明

The problem is : When I start the program, I cannot click on any buttons because when I drag the mouse in the tkinter window I have the "looping" / "waiting" cursor and it doesn't allow me to click on any buttons inside the interface.问题是:当我启动程序时,我无法点击任何按钮,因为当我在 tkinter 窗口中拖动鼠标时,我有“循环”/“等待”光标,它不允许我点击里面的任何按钮界面。

I tried to start the infinit loop function in a new thread, but I don't know if it is really a good way to solve my problem... Any of you have any solutions ?我试图在一个新线程中启动 infinit 循环函数,但我不知道这是否真的是解决我的问题的好方法......你们有什么解决方案吗?

I try to simulate my problem with a simple code :我尝试用一​​个简单的代码来模拟我的问题:

from tkinter import *
import loop


window = Tk()
window.title("test")

start = Button(window, text="start", bg ="green", command=loop.start)
stop  = Button(window, text="stop", bg ="green", command=loop.stop)
thread = Button(window, text="threads", bg ="green", command=loop.how_many_threads)

start.grid(column = 1, row = 1)
stop.grid(column = 2, row = 1)
thread.grid(column = 3 , row = 1)



window.mainloop()

and few functions :和几个功能:

import threading

continu = True



def infinit(x):
    cpt = 0
    while(x):
        cpt += 1
        #print(cpt)

    print("loop is over !")

def start():
    threadSimulation = threading.Thread(target= infinit(continu)).start()

def stop():
    self.continu = False

def how_many_threads():
    for t in threading.enumerate():
        print("thread names : ",t.getName())

Here's how to fix your implementation.以下是修复您的实现的方法。 The biggest error is that you have to provide arguments to the thread in the args part, not like a normal function call.最大的错误是你必须在args部分为线程提供参数,而不是像普通的函数调用那样。

import tkinter as tk
import threading
import time

def infinit(x):
    cpt = 0
    while(continu):
        cpt += 1
        print(cpt)
        time.sleep(1)
    print("loop is over !")

def start():
    threadSimulation = threading.Thread(target= infinit, args=(continu,))
    threadSimulation.daemon = True
    threadSimulation.start()

def stop():
    global continu
    continu = False

def how_many_threads():
    for t in threading.enumerate():
        print("thread names : ",t.getName())

continu = True

window = tk.Tk()
window.title("test")

start = tk.Button(window, text="start", bg ="green", command=start)
stop  = tk.Button(window, text="stop", bg ="green", command=stop)
thread = tk.Button(window, text="threads", bg ="green", command=how_many_threads)

start.grid(column = 1, row = 1)
stop.grid(column = 2, row = 1)
thread.grid(column = 3 , row = 1)

window.mainloop()

如果要关闭程序,可以在按钮的命令部分使用“window.quit”,如下所示:

stop = Button(window, text="stop", bg ="green", command=window.quit)

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

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