How to print output from a script in gui called in another Tkinter script?

后端 未结 2 1901
隐瞒了意图╮
隐瞒了意图╮ 2021-02-11 01:49

I have tried using several different similar solutions that I have found online, but none seem to quite do what I am aiming for.

I want to call an external script (hel

2条回答
  •  终归单人心
    2021-02-11 02:04

    In a method when a line with return ... is run, nothing else will be seen that comes after that line, as in your 2nd line of return ... is effectively useless as return cooz() is run unconditionally. You could simply replace your main with:

    def main():
        return cooz(), tooz()
    

    and accordingly your printSomething:

    x, y = helloworld.main()
    

    Returning all methods/functions from a script without explicitly passing method names:

    Well, I stand corrected, based on this answer you can do it fairly simply. For calling all methods or functions this answer helped a lot.

    Let's say there's a script named hello_world.py:

    def hello_world():
        print("Hello World!")
        print("this is the 2nd line of this method")
    
    def multiplication(*args):
        mult = 1
        for arg in args:
            mult *= arg
    
        return mult
    
    def some_other_method():
        print("some other method")
        print(multiplication(2, 3, 5, 7))
    

    is in the same directory as GUI script below:

    import tkinter as tk    # required for the GUI
    import subprocess       # required for redirecting stdout to GUI
    import sys, inspect     # required for all methods and functions redirection
    import hello_world      # the script file that is redirected
    
    def redirect(module, method):
        '''Redirects stdout from the method or function in module as a string.'''
        proc = subprocess.Popen(["python", "-c",
            "import " + module.__name__ + ";" + module.__name__ + "." + method + "()"],
                                                                    stdout=subprocess.PIPE)
        out = proc.communicate()[0]
        return out.decode('unicode_escape')
    
    def redirect_module(module):
        '''Retruns all stdout from all methods or functions in module as a string.'''
        # to filter out non-method, and non-function attributes
        all_mtds_or_funcs = inspect.getmembers(sys.modules[module.__name__], 
                                                    inspect.isfunction or inspect.ismethod)
        red_str_buffer = ""
        for method in all_mtds_or_funcs:
            red_str_buffer += redirect(module, method[0]) + "\n---\n"
    
        return red_str_buffer
    
    def put_in_txt(module):
        '''Puts the redirected string in a text.'''
        txt.insert('1.0', redirect_module(module))
    
    root = tk.Tk()
    txt = tk.Text(root)
    btn = tk.Button(root, text="Redirect")
    btn['command'] = lambda module=hello_world : put_in_txt(module)
    
    txt.pack()
    btn.pack()
    
    root.mainloop()
    

    which returns console output of all methods and functions in hello_world.py as a string. Based on this suggestion it then puts that string in a Text field.

提交回复
热议问题