适用于 python 而不适用于 pythonw 的代码 [英] Code that works with python and not with pythonw

查看:42
本文介绍了适用于 python 而不适用于 pythonw 的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个脚本,我希望它在后台运行并每 6 小时显示一次.我不想一直打开一个控制台,我希望 tkinter 弹出一个窗口,在该窗口中打印脚本的输出,然后我可以将其关闭,并在 6 小时内执行相同的操作.

I am writing a script and I want it to run in the back ground and to manifest itself every 6 hours. I don't want to have an opened console all the time, I want tkinter to pop a window in which it prints the output of the script that i can then close and that will do the same in 6 hours.

这是我的代码.

from datetime import datetime
import time
from tkinter import Tk, Label

dict_n = {}

def func():
    def check():
        today = datetime.today()
        a = str(today.day) + "/" + str(today.month)
        li_b = []
        li_c = []
        li_l = []
        li_k = []
        for i, j in dict_n.items():
            l = j.replace(" ","")[:-5]
            li_l.append(l)
            if l == a:
                c = 0b1
                li_b.append(i)
                li_c.append(c)
                li_k.append(j[-4:])
            else:
                c = 0b0
                li_c.append(c)
        k = str(today.year)
        return a, li_c, li_b, k, li_k
    date, li_bit, li_names, k, li_k = check()
    v = "Hi!"
    v += ("string " + date + "\n")
    maskb = 0b1
    d = 0
    for p in li_bit:
        if p & maskb == 0:
            d += 0
        if p & maskb != 0:
            m = int(k) - int(li_k[d])
            v += ("string" + li_b[d] + str(m))
            d += 1
    if d == 0:
        v += ("string")
    return v

def main():
    root = Tk()
    test = func()
    w = Label(root, text=test)
    w.pack()
    root.mainloop() 
    g = 1
    while g != 2:
        root = Tk()
        time.sleep(21600)
        retest = func()
        h = Label(root, text=retest)
        h.pack()
        root.mainloop()  

if __name__ == '__main__':
    main()  

问题是:只要我使用 python.exe,它就可以完美运行.但由于我不想打开控制台,我想使用 pythonw.exe.然后它不起作用.我说它不起作用是当我通过简单的双击从我的桌面执行脚本时没有任何反应.(与使用 python.exe 完全相反,它的行为与我希望的行为完全一样,每 6 小时,一个窗口弹出,其中打印了func"的输出)很抱歉有大量代码,但我听说有些操作在没有控制台的情况下无法运行,我不知道哪个操作会出现此问题.

The problem is : As long as I use python.exe it works perfectly. But since I don't want to have the console open I would like to use pythonw.exe. And then it does not work. Whan I say it does not work is that when I execute the script from my desktop by simple double clicking nothing happens. (as opposed to the use of python.exe which behaves exactly how I want it to behave, every 6 hours, a window pops open with the output of "func" printed in it) Sorry for the large amount of code but I heard that some operations don't run without a console and I have no clue which operation could have this problem.

你能帮我找出问题吗.

卡普罗

推荐答案

我不知道为什么你的代码不起作用",但我不知道你的意思.但是,您肯定在代码中做了一些非常错误的事情,应该会阻止它在任何情况下工作.我发现无论你如何运行它,都很难相信它是合理的.

I don't know why your code "doesn't work", but I don't know what you mean by that. However, you're definitely doing some very wrong things in your code that should prevent it from working in any circumstance. I find it hard to believe this works reasonably no matter how you run it.

您首先在无限循环之前调用 mainloop(因为您从未将 g 设置为 2),因此在您销毁创建的窗口之前该循环不会运行.然后,一旦原始窗口被销毁,您就会进入一个循环,在该循环中您在每次迭代时调用 mainloop.同样,在窗口被销毁之前,mainloop 不会退出,因此循环要求您一遍又一遍地销毁窗口.

You first call mainloop before an infinite loop (because you never set g to 2), so that loop won't run until you destroy the window that is created. Then, once the original window is destroyed you enter a loop where you call mainloop on each iteration. Again, mainloop won't exit until the window is destroyed, so the loop requires that you keep destroying the window over and over and over.

Tkinter 旨在以特定方式使用,即创建 Tk单个实例,并且只调用一次 mainloop.除非您深入了解 Tkinter 的工作原理,否则其他任何事情都会给您带来一些意想不到的行为.

Tkinter is designed to be used in a particular way, which is to create a single instance of Tk, and call mainloop exactly once. Anything else will give you somewhat unexpected behavior unless you deeply understand how Tkinter works.

这篇关于适用于 python 而不适用于 pythonw 的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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