Code that works with python and not with pythonw

2019-08-29 15:48发布

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.

There is my code.

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()  

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.

Could you help me identify the problem please.

Capurot

1条回答
看我几分像从前
2楼-- · 2019-08-29 16:53

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.

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 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.

查看更多
登录 后发表回答