0

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

Dronan
  • 5
  • 3
  • What does "does not work" mean? Do you get an error? Is it really working, but simply not working how you think it should? – Bryan Oakley Jul 28 '13 at 15:55
  • Have you tried going to a console, and typing "pythonw my_script.py"? Doing that might show you some errors. – Bryan Oakley Jul 28 '13 at 16:41
  • ok, so I just did and the results are interesting in that if I type pythonw my_script.py it only opens a window with my function's output once and then never repeats just as if I had closed the console. But when I type pythonw my_script.pyw then it works exactly like I want it . The window pops open every set time and the timer resets when I close the window. For some reason when I try to execute it by double clicking even though I made sur I used the same path to the pythonw.exe as in the console it does not work. – Dronan Jul 28 '13 at 17:23
  • So I tried different things and the only way to make the script work with pythonw is to execute C://filepath/pythonw.exe c://filepath/my_script.pyw. – Dronan Jul 28 '13 at 18:44

1 Answers1

0

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.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • I am fairly new to python and even newer to tkinter (discovered it today). What I mean is that I am well aware that basically nothing is do is the optimal way to do things, but, the infinite isn't loop a genuine way of looping the script evrey 6 hours to do the exact same thing and also to not open multiple windows if I am not at my desk since the loop can only continue if I close the window ? any suggestion is obviously more than welcome. I'll definitely edit the original to explain precisely "how" it does not work, thanks – Dronan Jul 28 '13 at 16:13