将stdout重定向到多个输出小部件

时间:2016-02-18 23:06:53

标签: csv python-3.x tkinter stdout

我有一个搜索CSV文件的程序,并通过stdout文本方向将整行输出到单个输出小部件。我想要的是从这一行拆分一些字段,以便一个字段转到一个输出窗口小部件,另一个字段转到另一个窗口小部件等。我通过一些循环技术推测它,但不完全确定该怎么做。

搜索CSV的代码,如果有帮助,则会重定向到单个输出小部件。

#output
    self.label4 = ttk.Label(rootWindow, text="Drawings:")
    self.label4.grid(row=3, column=1, sticky=(N,E), padx=3, pady=3)

    self.dwgoutputbox = Text(rootWindow, width=50, height=15, background="white", font="system_font 10")
    self.dwgoutputbox.grid(row=3, column=2, columnspan=5, padx=3, pady=3)
    sys.stdout = TextRedirector(self.dwgoutputbox, "printdwgoutputbox") 

    #buttons
    def DoASearch():
        self.outputQty.delete(1.0, 'end')
        self.outputDesc.delete(1.0, 'end')
        self.dwgoutputbox.configure(state="normal")
        self.dwgoutputbox.delete(1.0, 'end')
        self.dwgoutputbox.configure(state="disabled")
        try:
            (int(sonumber.get()))
            checkcsv()
        except ValueError:
            messagebox.showwarning("Fail !!", "Please enter a valid Shop Order number.")

    def checkcsv():
        with open("spreadsheettest.csv") as csvfile:
            reader = csv.DictReader(csvfile)
            for row in reader:
                result=(row['Shop Order'])
                if sonumber.get() == result:
                    descQty=(row['Quantity'])
                    descInfo=(row['Description'])
                    descPN=(row['Part Number'])
                    descDwg=(row['Drawings'])

    self.searchbutton = ttk.Button(rootWindow, text="Search", command=DoASearch)
    self.searchbutton.grid(row=1, column=7, sticky=W, padx=3, pady=3)


    def clear_text():
        self.SOentry.delete(0, 'end')
        self.outputQty.delete(1.0, 'end')
        self.outputDesc.delete(1.0, 'end')
        self.dwgoutputbox.configure(state="normal")
        self.dwgoutputbox.delete(1.0, 'end')
        self.dwgoutputbox.configure(state="disabled")

    self.clrsearchbutton = ttk.Button(rootWindow, text="Reset", command=clear_text)
    self.clrsearchbutton.grid(row=2, column=7, sticky=W, padx=3, pady=3)

    self.exitbutton = ttk.Button(rootWindow, text="Exit", command=rootWindow.destroy)
    self.exitbutton.grid(row=3, column=7, sticky=(N,W), padx=3, pady=3)


class TextRedirector(object):
def __init__(self, widget, tag="printdwgoutputbox"):
    self.widget = widget
    self.tag = tag

def write(self, str):
    self.widget.configure(state="normal")
    self.widget.insert("end", str, (self.tag,))
    self.widget.configure(state="disabled")

我已经按照下面的建议修改了代码,并且在我更改输入文本小部件中的数据时,它似乎工作得很好并且更新

        def checkcsv():
        with open("lesspreadsheettest.csv") as csvfile:
            reader = csv.DictReader(csvfile)
            for row in reader:
                result=(row['Shop Order'])
                if sonumber.get() == result:
                    descQty=(row['Quantity'])
                    descInfo=(row['Description'])
                    descPN=(row['Part Number'])
                    descDwg=(row['Drawings'])
                    self.outputQty.insert(INSERT,descQty)
                    self.outputDesc.insert(INSERT,descPN)
                    self.outputDesc.insert(INSERT,descInfo)
                    self.dwgoutputbox.insert(INSERT,descDwg)

    self.searchbutton = ttk.Button(rootWindow, text="Search", command=DoASearch)
    self.searchbutton.grid(row=1, column=7, sticky=W, padx=3, pady=3)

0 个答案:

没有答案