Python Forum
[Tkinter] Defining a self made module's function to interact with the main .py variables?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Defining a self made module's function to interact with the main .py variables?
#5
Thanks for replying :)

Since my last reply i've added multiprocessing and threads so now i'm having some difficulties with your answer because I have a thread running
a process handler that runs the function.

main.py:

Text widget:

    def text_widget(self):

        self.text_window = tk.Text(self, height=100, width=500, bg="gray", wrap=tk.WORD)
        self.text_window.update()
        self.text_window.grid(row=7, column=1, rowspan=3, sticky="we")
        self.text_window.insert(tk.END, ">>>")
        self.thread_update_text_widget_default()

    def thread_update_text_widget_default(self):
        ud = threading.Thread(name="Updating Text Widget from file",
                              target=self.update_text_widget_default, args=())
        ud.daemon = False
        ud.start()

    def update_text_widget_default(self):
        
        print("Updating Text Widget from file")
        file = fr'c:/users/{os.getlogin()}/Desktop/Gui-Skeleton/default-tmp.txt'
        with open(file, 'r') as f:
            console_text = f.readline()
            for line in console_text:
                self.text_window.delete('1.0', tk.END)
                self.text_window.insert(tk.END, f'>>>{line}')
    def thread_handler_clr_yes_fn_no(self, host):
        yn = threading.Thread(target=self.proc_handler_clr_yes_fn_no, args=(host,))
        yn.daemon = False
        yn.start()
    def proc_handler_clr_yes_fn_no(self, host):

        startprocs = []
        lastprocs = []

        proc1 = mp.Process(name="Clear + No Filename + WriteFile",
                           target=testping.clr_yes_fn_no_writefile, args=(host,))
        proc1.daemon = True
        proc2 = mp.Process(name="Clear + No Filename + PrintOutput",
                           target=testping.clr_yes_fn_no_print_output, args=(host,))
        proc3 = mp.Process(name="Clear + No Filename + Generate PrintOutput to GUI",
                           target=testping.clr_yes_fn_no_print_to_gui, args=(host,))
        proc4 = mp.Process(name="Remove first line + Write new default file",
                           target=testping.delete_default_lines)

        startprocs.append(proc1)
        startprocs.append(proc2)
        startprocs.append(proc3)

        lastprocs.append(proc4)

        for s in startprocs:
            s.start()

        for s2 in startprocs:
            s2.join()

        for l in lastprocs:
            l.start()
this is on testping.py:

def clr_yes_fn_no_print_to_gui(host, file=sys.stdout):

    print(f'Current Proccess: {mp.current_process().name} + {mp.current_process().pid}\n')
    cmd = ["ping", "-n", "4", f'{host}']
    proc = sub.Popen(cmd, stdout=sub.PIPE, shell=True, stderr=sub.STDOUT)
    for stdout_line in proc.stdout:
        print(stdout_line.decode(), end='', file=file)
    proc.stdout.close()
    return_code = proc.wait()
    if return_code:
        raise sub.CalledProcessError(return_code, cmd)
as for GIT, i'm now using google drive and a local repo but i'll definetly pivot to GIT soon.
Reply


Messages In This Thread
RE: How can I define a selfmade module's functio to interact with the main .py variables? - by Gilush - Jun-07-2020, 02:39 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How can I run an interface I made using PyQt5 and a Map program I made using Tkinter bki 6 1,351 Nov-09-2023, 11:12 PM
Last Post: bki
  [PyQt] Managing variables accross a multiple module GUI Oolongtea 18 11,115 Sep-19-2019, 12:13 PM
Last Post: Oolongtea
  “main thread is not in main loop” in Tkinter Long_r 1 24,505 Jun-26-2019, 11:00 PM
Last Post: metulburr
  [Tkinter] Bringing function out of class into main loop zukochew 1 2,721 Jul-30-2018, 06:43 PM
Last Post: Axel_Erfurt
  GTK main window calling a main window DennisT 4 6,969 Oct-19-2016, 09:36 PM
Last Post: DennisT

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020