我想在单击按钮时在tkinter脚本的main.py脚本中重定向调用函数的输出

时间:2018-08-10 11:01:15

标签: python python-3.x tkinter subprocess

我想在单击按钮时在tkinter脚本的main.py脚本中重定向调用函数的输出。我的问题是something like this 我已经尝试了所有答案,但是它们似乎对我没有用,因为我想获取特定函数的输出。 PS:我的主模块功能基本上是从IOT设备读取数据并进行打印。 我将分享我的tkinter脚本。我对python和gui都还很陌生,所以请多多包涵。

import tkinter as tk
import main
import subprocess
import sys, inspect 
import threading
import warnings


def get_serial_no():


                #global input_sl_no
                #input_sl_no = input("Enter Serial Number\t")
    input_sl_no=str(entry_serial.get())

    input_sl_no = input_sl_no.upper()
    print("this is the number {}".format(input_sl_no))

    if(len(input_sl_no) != 16 or (input_sl_no.find("SAM",0,len(input_sl_no)) == -1)):
        print("Enter Valid Serial Number\t")


    return input_sl_no



def func2():

    entry_username.delete(0, tk.END)
    entry_serial.delete(0, tk.END)

def func3():

    root.destroy()
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(main))    #entry_serial.delete(0, tk.END)

if __name__ == '__main__':
    root=tk.Tk()
    root.title('BASIC TESTING')
    #root.geometry('{}x{}'.format(460, 350))
    top_frame = tk.Frame(root, bg='cyan',width=600, height=600)
    center = tk.Frame(root, bg='gray2', width=50, height=40, padx=3, pady=3)
    #top_frame.pack(fill="both", expand=True)
    top_frame.grid_rowconfigure(1, weight=1)
    top_frame.grid_columnconfigure(0, weight=1)

    top_frame.grid(sticky='nsew')
    center.grid(sticky="nsew")


    sl_no = tk.Label(top_frame, text='username:')
    username=tk.Label(top_frame, text='serial number:')
    entry_username = tk.Entry(top_frame, background="orange")
    entry_serial = tk.Entry(top_frame, background="orange")

    sl_no.grid(row=0)
    username.grid(row=1)
    entry_username.grid(row=0, column=1)
    entry_serial.grid(row=1, column=1)
    start_btn= tk.Button(top_frame, text="start", command=lambda module=main.testing() : put_in_txt(module))
    start_btn.grid(columnspan=2)
    goback_btn1= tk.Button(top_frame, text="go back", command=func2)
    goback_btn1.grid(columnspan=2)
    quit_btn2= tk.Button( top_frame,text="start", command=func3)
    quit_btn2.grid(columnspan=2)
    txt = tk.Text(center,borderwidth=3, relief="sunken")
    txt.config(font=("consolas", 12), undo=True, wrap='word')
    txt.grid(row=0, column=0, sticky="nsew", padx=2, pady=2)
    vsb = tk.Scrollbar(center, orient="vertical", command=txt.yview)
    txt.configure(yscrollcommand=vsb.set)
    vsb.grid(row=0, column=0, sticky="nsew")


    root.mainloop()

它抛出模块未找到错误

0 个答案:

没有答案