Home>

I plan to make a simple app with python3.

Watch the folder and create a new text file
It is to do some processing in that file.

To monitor the folder there
Usewhileto monitor folders
Variable to updateupdate_intarbal
watch_mode
Ifwatch_modeis 1, watch it, and exit if 0
I'm thinking of assembling it.

is

Launch the app
[App launch]

[Start tkinter frame (Label: Monitor mode andButton: STOP)]

[while looping (watch_mode = 1for monitoring)]

Exit the app
[Update towatch_mode = 0by pressingButton: STOP in the tkinter frame]

[Exit the while loop (watch_mode = 0)]

[App stop]

If I want to stop, press tkinter's STOP button
It feels likewatch_modeis updated to 0 and exits the while loop

I managed to write the following code, but it was far from ideal ...
For now,tkinterandwhiledon't work together
Also, even if you press the stop button, watch_mode is not updated and it becomes an infinite loop.

If i have any knowledge, please give me advice.

import tkinter as tk
from time import sleep
def main ():
    watch_mode = 1 #Automatic conversion mode
    while_loop = 0 #Manage initial behavior
    update_intarval = 5 #Update time (s)
    while watch_mode == 1:
        if while_loop == 0: Only run # 1
            while_loop = 1
            showinfo = tk.Tk ()
            frame_top = tk.Frame (showinfo, bd = 2, relief = "ridge")
            frame_top.pack (fill = "x", pady = 2)
            label1 = tk.Label (frame_top, text = "It is a monitoring mode")
            label1.pack (side = "left")
            frame_bottom = tk.Frame (showinfo, bd = 2)
            frame_bottom.pack (fill = "x")
            def stop_convert ():
                watch_mode = 0
                showinfo.destroy ()
            button1 = tk.Button (frame_bottom, text = "STOP", bg = "# f0e68c", fg = "# ff0000", command = stop_convert)
            button1.pack ()
            showinfo.mainloop ()
        sleep (update_intarval)
        print (watch_mode)
if __name__ == '__main__':
    main ()
  • Answer # 1

    I tried using.afteroftkinteras advised.
    It became the movement that I thought of for the time being.

    Here's the code.

    Please give me any advice. m (_ _) m

    import tkinter as tk
    def main ():
        after_id = None
        repet_nums = 0
        update_intarval = 100 #Update time (msec)
        def convert_data ():
            global after_id
            #### Write the operation you want to repeat in the lower part #################################### #####
            global repet_nums
            repet_nums + = 1
            print ("If you look at the folder and you have a new file, you can convert it", repet_nums)
            if repet_nums == 1:
                print ("ikkaime")
            ################################################## ###########################
            after_id = showinfo.after (update_intarval, convert_data)
        def start ():
            global repet_nums
            repet_nums = 0
            convert_data ()
        def stop_convert ():
            global after_id
            if after_id:
             showinfo.after_cancel (after_id)
             after_id = None
             showinfo.destroy ()
        showinfo = tk.Tk ()
        frame_top = tk.Frame (showinfo, bd = 2, relief = "ridge")
        frame_top.pack (fill = "x", pady = 2)
        label1 = tk.Label (frame_top, text = "It is a monitoring mode")
        label1.pack (side = "left")
        frame_bottom = tk.Frame (showinfo, bd = 2)
        frame_bottom.pack (fill = "x")
        button1 = tk.Button (frame_bottom, text = "STOP", bg = "# f0e68c", fg = "# ff0000", command = stop_convert)
        button1.pack ()
        start ()
        showinfo.mainloop ()
    if __name__ == '__main__':
        main ()