我正在尝试构建一个简单的程序来提醒我在使用计算机时休息一下.我对

python有一个合理的理解,但以前从未玩过GUI编程或线程,所以以下基本上是从stackoverflow复制/粘贴:

import threading

import time

import Tkinter

class RepeatEvery(threading.Thread):

def __init__(self, interval, func, *args, **kwargs):

threading.Thread.__init__(self)

self.interval = interval # seconds between calls

self.func = func # function to call

self.args = args # optional positional argument(s) for call

self.kwargs = kwargs # optional keyword argument(s) for call

self.runable = True

def run(self):

while self.runable:

self.func(*self.args, **self.kwargs)

time.sleep(self.interval)

def stop(self):

self.runable = False

def microbreak():

root = Tkinter.Tk()

Tkinter.Frame(root, width=250, height=100).pack()

Tkinter.Label(root, text='Hello').place(x=10, y=10)

threading.Timer(3.0, root.destroy).start()

root.mainloop()

return()

thread = RepeatEvery(6, microbreak)

thread.start()

这给了我第一次中断通知但在给我第二次中断通知之前失败了.

Tcl_AsyncDelete: async handler deleted by the wrong thread

fish: Job 1, “python Documents/python/timer/timer.py ” terminated by

signal SIGABRT (Abort)

有任何想法吗?我很乐意使用除了tkinter之外的其他东西用于gui-stuff或者除了线程以外的东西来实现时间的东西.

根据以下答案,我的新工作脚本如下:

import Tkinter as Tk

import time

class Window:

def __init__(self):

self.root = None

self.hide = 10 #minutes

self.show = 10 #seconds

def close(self):

self.root.destroy()

return

def new(self):

self.root = Tk.Tk()

self.root.overrideredirect(True)

self.root.geometry("{0}x{1}+0+0".format(self.root.winfo_screenwidth(), self.root.winfo_screenheight()))

self.root.configure(bg='black')

Tk.Label(self.root, text='Hello', fg='white', bg='black', font=('Helvetica', 30)).place(anchor='center', relx=0.5, rely=0.5)

#Tk.Button(text = 'click to end', command = self.close).pack()

self.root.after(self.show*1000, self.loop)

def loop(self):

if self.root:

self.root.destroy()

time.sleep(self.hide*60)

self.new()

self.root.mainloop()

return

Window().loop()

最佳答案 我认为没有线程就可以更轻松地实现这一点,而Tkinter并没有很好地集成.相反,您可以使用after和after_idle方法来安排在特定超时后运行的回调.您可以创建一个显示窗口的方法并将其计划为隐藏窗口,另一个方法隐藏窗口并安排窗口显示.然后他们只会在无限循环中互相称呼:

import Tkinter

class Reminder(object):

def __init__(self, show_interval=3, hide_interval=6):

self.hide_int = hide_interval # In seconds

self.show_int = show_interval # In seconds

self.root = Tkinter.Tk()

Tkinter.Frame(self.root, width=250, height=100).pack()

Tkinter.Label(self.root, text='Hello').place(x=10, y=10)

self.root.after_idle(self.show) # Schedules self.show() to be called when the mainloop starts

def hide(self):

self.root.withdraw() # Hide the window

self.root.after(1000 * self.hide_int, self.show) # Schedule self.show() in hide_int seconds

def show(self):

self.root.deiconify() # Show the window

self.root.after(1000 * self.show_int, self.hide) # Schedule self.hide in show_int seconds

def start(self):

self.root.mainloop()

if __name__ == "__main__":

r = Reminder()

r.start()

python自动关闭弹窗_python – 每隔x分钟创建一个tkinter窗口,然后在y秒后自动关闭它...相关推荐

  1. html弹窗后 自动关闭页面,网页一键复制弹出提示窗口后几秒后自动关闭提示js代码...

    L 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> ...

  2. 3分钟创建一个游戏类容器应用【华为云分享】

    [摘要] 云容器引擎(Cloud Container Engine)新手课堂 - 本期导读:3分钟创建一个2048游戏类容器应用. 在学习本期CCE新手课堂前,建议您先学习以下课程: 1. CCE基本 ...

  3. python生成一个窗口_PyQt5创建一个新窗口的实例

    更多python教程请到友情连接: 菜鸟教程www.piaodoo.com 人人影视www.sfkyty.com 飞卢小说网www.591319.com 韩剧网www.op-kg.com 兴化论坛ww ...

  4. python使用np.logspace函数在对数刻度上创建一个对数等距数组实战:在对数刻度上创建一个数组(指定数值个数以及是否包含末尾界值)、使用不同的基数(底数)在对数刻度上构建等距数组、可视化

    python使用np.logspace函数在对数刻度上创建一个对数等距数组实战:在对数刻度上创建一个数组(指定数值个数以及是否包含末尾界值).使用不同的基数(底数)在对数刻度上构建等距数组.可视化 目 ...

  5. python打开一个不存在的文件时-python判断文件是否存在,不存在就创建一个的实例...

    python判断文件是否存在,不存在就创建一个的实例 如下所示: try: f =open("D:/1.txt",'r') f.close() except IOError: f ...

  6. Android几秒后自动关闭dialog

    代码改变世界 Android几秒后自动关闭dialog AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());bu ...

  7. 【超直白】三分钟创建一个小程序

    让我们花三分钟创建一个小程序! 注册账号 访问微信公众平台网站:https://mp.weixin.qq.com 鼠标放到小程序上 发现图案变成了,下面这样,点击[查看详情],进入小程序注册页面. 拉 ...

  8. js案例:5秒后自动关闭

    分析 核心思路:5秒之后把广告隐藏起来 定时器setTimeout() 代码 <html> <head> <meta charset="utf-8"& ...

  9. python定位弹窗_python定位弹窗

    广告关闭 提供包括云服务器,云数据库在内的50+款云计算产品.打造一站式的云产品试用服务,助力开发者和企业零门槛上云. 前言用真机运行appium代码,首次打开app有的手机会出现权限弹窗问题,一般这 ...

最新文章

  1. 黄铁军:沉迷于寻求通用智能理论,将是阻碍 AI 发展的最大障碍 | Yann LeCun 自传《科学之路》序...
  2. 民生银行IT运维架构管理可视化实践
  3. 2022速卖通328大促活动招商规则出炉啦,热销高潜产品抢先看
  4. MySql的连接查询
  5. [css] CSS content属性特殊字符有哪些?
  6. 多个集合合并成没有交集的集合-实现
  7. 接口进阶 java 1614956772
  8. 解决IDEA不能编译XML文件
  9. 马士兵java教程笔记_马士兵java教程笔记4
  10. python读取文件乱码
  11. Nginx + uWSGI + Flask + Vhost
  12. GDAL源码剖析(四)之命令行程序说明二
  13. JS的IE和FF兼容性问题汇总
  14. git add 所有修改文件_工作中Git的使用实践
  15. 基于javaweb+mysql数据库实现的宠物领养|流浪猫狗网站项目源代码
  16. 哈工大2022形式语言与自动机期末
  17. C++描述 LeetCode 480. 滑动窗口中位数
  18. 高手教你如何从零开始学游戏编程
  19. 青岛市中小学学籍系统服务器,全国各省市中小学学籍网登录入口
  20. 如何学好游戏编程 二

热门文章

  1. 计算机网络学习笔记(一)——分层模型、协议、服务、连接模式、标准化组织
  2. 知乎万赞:人并不是活一辈子,而是活几个瞬间
  3. AI 从业者都会用到的 10 个深度学习方法
  4. 利用GAN实现QR Code超分辨率的研究
  5. 《论可计算数及其在判定上的应用》简单理解
  6. python文件操作以及相对路径和绝对路径问题
  7. Cloudera CDP 企业数据云测试开通指导
  8. Spark 大数据处理最佳实践
  9. 高并发库存秒杀场景,阿里巴巴数据库是这样应对的
  10. 学不动?Apache Member 教你评估实用技术的思路