日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

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

發布時間:2024/9/27 python 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python自动关闭弹窗_python – 每隔x分钟创建一个tkinter窗口,然后在y秒后自动关闭它... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

我正在嘗試構建一個簡單的程序來提醒我在使用計算機時休息一下.我對

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秒后自动关闭它...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。