程序启动后按钮不起作用的问题 [英] Issue with buttons not functioning after start of program

查看:55
本文介绍了程序启动后按钮不起作用的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新手,正在学习 Python 3.6,我几乎完成了我的第一个代码项目.在进行了详尽的搜索以解决我的问题后,我一直无法找到答案,我确信这是一个简单的疏忽.

I am new and learning python 3.6 and I've almost completed my first code project. After doing an exhaustive search to resolve my problem I have not been able to find the answer to what I am sure is a simple oversight.

方案前提:用户输入 URL 并单击时间范围的复选框以调用该 URL.然后用户点击开始按钮,程序开始根据选择的复选框(时间)在时间循环中打开 url.这工作正常......但是一旦程序启动,按钮不再处于活动状态并且不起作用,它们无法被点击.我已经将 tkinter 用于 gui.

Program premise: A user enters a URL and also clicks in a checkbox for the time frame to call the url. The user then clicks the start button and the program begins to open the url in a timeloop according to the checkbox(time) selected. This is working fine... however once the program has started the buttons are no longer active and do not work, they are not able to be clicked. I have used tkinter for the gui.

我正在尝试为用户保持 2 个按钮处于活动状态.退出按钮,退出程序的功能,退出按钮,退出并完全关闭程序.

I am trying to keep 2 buttons active for the user. The Quit button, which quits the function of the program and the Exit button that exits and closes the program completely.

指出正确方向的点将不胜感激.

A point in the right direction would be appreciated.

当前使用的 sleep() 代码:

Current sleep() code used:

def execute():
execute = urllib.request.urlopen(url.get()))

def Time():
if CheckVar1.get() and url.get():
timeloop = True
Sec = 0
Min = 0
timeLoop = A
while timeLoop:
    Sec += 1
    execute()
    time.sleep(300)

我希望这有助于让我的问题更容易理解.我相信我可能需要找到另一种方法来执行此操作,以便循环允许程序的其他部分(按钮)保持活动状态,以便用户可以在程序运行后退出或退出程序.注意:CheckVar1.get() 用于验证是否已为该时间范围选中复选框,有 8 种不同的时间设置正在使用.

I hope this helps make my question a little easier to follow. I believe that I might need to find another way to do what this is doing so that the loop allows the other parts of the program (buttons), to remain active so a user can Quit or Exit the program once it is running. Note: CheckVar1.get() is to verify if the checkbox has been selected for that time frame, there are 8 different time settings being used.

推荐答案

你的代码缩进是错误的,也许还有其他问题,但由于你的方法本质上是错误的,这里有一个例子,说明如何在一些之后调用函数在 tkinter 中的时间,而不会阻塞 UI.这个可以作为一种后台处理,很简单:

Your code indentation is wrong and maybe there are other issues too, but as your approach is essentially wrong, here is an example of how to call a fuction after some time in tkinter, without blocking the UI. This can be used as a kind of background processing, and it's simple:

import tkinter as tk
from tkinter import Tk, ttk


def some_action():
    # do what you want
    # ...
    print('Hi! Getting my urls here...')
    # but don't block with sleeps or infinite loops,
    # and then
    next_check = get_your_check_interval() # somehow calculate or
                                           # retrieve this from
                                           # your vars or widgets,
                                           # using seconds
    # register for the next call of some_action
    root.after(next_check*1000, some_action)

def get_your_check_interval():
    return 3 # just using 3 seconds now...

root = Tk()

start_button = ttk.Button(root, text='Start', command=some_action)
quit_button = ttk.Button(root, text='Quit', command=quit)


start_button.pack(side=tk.LEFT)
quit_button.pack()
root.mainloop()

如您所见,按钮保持响应,您可以退出应用程序.所以我认为你可以用同样的技术解决你的问题.

As you see the buttons remain responsive and you can quit the application. So I think you can solve your problem with the same technique.

这篇关于程序启动后按钮不起作用的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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