【问题标题】:win10toast causes tkinter window freezewin10toast 导致 tkinter 窗口冻结
【发布时间】:2020-09-13 11:07:18
【问题描述】:

我是python的新手,正在尝试练习, 经过数小时的检查,我了解到我的程序的问题在于win10toasttkinter window 似乎有问题。

# -*- coding: utf-8 -*-
"""
Created on Sun May 24 18:18:00 2020

@author: MeTaNa
"""

'''
this program is simple, notifys u if battery is fully charged,
'''

from tkinter import Label, Tk, Button
import os
import psutil
import win10toast

# current_statues = percent + '% | ' + plugged


def preload():
    pass

def start():
    global percent
    global plugged
    battery = psutil.sensors_battery()
    plugged = battery.power_plugged
    percent = str(battery.percent)
    if plugged == False:
        plugged = "Not Plugged In"
    else:
        plugged = "Plugged In"
    if (psutil.sensors_battery().power_plugged == True) and (battery.percent == 100):
        print(percent + '% | ' + plugged)
        print('Unplug the Charger Please!')
        win10toast.ToastNotifier().show_toast('Battery Statues', 'Battery Full.\nUnplug the Charger Please!', icon_path='', duration=10)
        gui.after(5000, start)
    elif (psutil.sensors_battery().power_plugged == False) and (battery.percent != 100):
        print(percent + '% | ' + plugged)
        print('Not Charging...')
        win10toast.ToastNotifier().show_toast('Battery Statues', 'Charger Not Plugged', icon_path='', duration=10)
        gui.after(5000, start)
    else:
        print(percent + '% | ' + plugged)
        print('Charging...')
        win10toast.ToastNotifier().show_toast('Battery Statues', 'Charging...', icon_path='', duration=10)
        gui.after(5000, start)



def closer():
    try:
        os.system('TASKKILL /F /IM Bat2.exe')
    except Exception:
        print('already closed!')
    gui.destroy()



# create a GUI window
gui = Tk()
gui.resizable(False, False)
gui.geometry("200x200")
gui.configure(background="white")
gui.title("Battery Notifier")

battey_statues = Label(gui, text='percent')
battey_statues.pack()

button1 = Button(gui, text=' Start Script ', fg='white', bg='gray', command=start, height=2, width=9)
button1.pack()
button2 = Button(gui, text=' Stop Script ', fg='white', bg='gray', command=closer, height=2, width=9)
button2.pack()


# start the GUI
gui.after(100, preload)
gui.mainloop()

当我按下start script 按钮时,它会调用start 函数,但它会冻结 tkinter 窗口,我不明白为什么。

之前我遇到了 tkinter 和 while loop 的问题,我用 after 方法解决了。

  • 脚本无需 tkinter 即可正常运行。

所以,有什么想法我该怎么办?

【问题讨论】:

    标签: python python-3.x tkinter


    【解决方案1】:

    查看 win10toast (https://github.com/jithurjacob/Windows-10-Toast-Notifications/blob/master/win10toast/init.py) 的源代码。普通选项是无线程的,这将导致 gui 挂起,但它也支持线程选项。

    尝试使用以下内容开始您的通知

    win10toast.ToastNotifier().show_toast('Battery Statues', 'Charging...', icon_path='', duration=10, threaded=True)
    

    【讨论】:

    • 非常感谢,它现在可以工作了,那么,没有办法运行非线程吗?
    • @NimaMetana 为什么要运行“无线程”?添加线程部分使其工作。
    猜你喜欢
    • 2019-12-16
    • 1970-01-01
    • 2018-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多