如何使用Python tkinter在其他框架中的文本框中显示进程输出 [关闭]。

问题描述 投票:2回答:1

我试图将 "ping "的输出写到一个文本框中,这个文本框将在 "output "tkinter框架中创建,当我在将输出写到文件中时按下 "提交 "按钮,请问这是怎么回事?

问题是

1: 有什么方法可以把文本框放在 "输出 "框架内?2: 如何在 "输出 "框架内打印文件中的行?3: 如何使用线程或多进程来实时显示输出?

在点击 "提交 "之前。

before clicking Submit

import tkinter as tk
import subprocess as sub
from multiprocessing import Queue
import os

class GUI(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        self.geometry("650x500")
        self.title("Gil Shwartz GUI Project")
        menu = tk.Frame(self, height=250, width=10, relief="solid")
        menu.pack(side=tk.LEFT, fill="both", anchor="w")
        container = tk.Frame(self, relief="flat")
        container.pack(side=tk.TOP, fill="y", expand=True)
        output = tk.LabelFrame(self, text="Output", height=350, width=70)
        output.pack(side=tk.BOTTOM, fill="both", expand=True)

        menu.grid_columnconfigure(0, weight=1)
        menu.grid_rowconfigure(0, weight=1)

        self.frames = ["Menu", "MainWelcome", "testPing", "PageOne", "PageTwo"]

        self.frames[0] = Menu(parent=menu, controller=self)
        self.frames[1] = MainWelcome(parent=container, controller=self)
        self.frames[2] = testPing(parent=container, controller=self)
        self.frames[3] = PageOne(parent=container, controller=self)
        self.frames[4] = PageTwo(parent=container, controller=self)

        self.frames[0].grid(row=0, column=0, sticky="nsew")
        self.frames[1].grid(row=0, column=0, sticky="nsew")
        self.frames[2].grid(row=0, column=0, sticky="nsew")
        self.frames[3].grid(row=0, column=0, sticky="nsew")
        self.frames[4].grid(row=0, column=0, sticky="nsew")

        self.show_frame(1)


    def show_frame(self, page_name):
        frame = self.frames[page_name]
        print(frame)
        frame.tkraise()
        frame.grid(row=0, column=0, sticky="nsew")

class Menu(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller

        button1 = tk.Button(self, text="Ping Test",
                            command=lambda: controller.show_frame(2))
        button1.config(bg="royalblue2")
        button2 = tk.Button(self, text="Page Two",
                            command=lambda: controller.show_frame(4))
        button2.config(bg="dark violet")
        button3 = tk.Button(self, text="Quit",
                            command=lambda: Menu.terminate(self))
        button3.config(bg="gray40")

        button1.pack(fill="both", expand=True)
        button2.pack(fill="both", expand=True)
        button3.pack(fill="both", expand=True)
        button1.grid_columnconfigure(0, weight=1)
        button2.grid_columnconfigure(0, weight=0)
        button3.grid_rowconfigure(0, weight=0)

    def terminate(self):
        exit()

class MainWelcome(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller

        label = tk.Label(self, text="Text 1", bg="yellow")
        label.pack(fill="x", expand=True)
        label = tk.Label(self, text="Text 2", bg="yellow")
        label.pack(fill="x")

class testPing(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller

        urlLabel = tk.Label(self, text="Enter URL : ", padx=5, pady=5)
        urlLabel.pack(anchor="w")
        urlInputBox = tk.Entry(self)
        urlInputBox.pack(anchor="w")
        urlInputBox.grid_columnconfigure(0, weight=0)

        clearFileLabel = tk.Label(self, text="Clear File?", padx=5, pady=5)
        clearFileLabel.pack(anchor="w")

        clearFile = tk.BooleanVar()
        clearFile.set(False)
        clearFileRadioYes = tk.Radiobutton(self, text="yes", value=True, var=clearFile,
                                           command=lambda: self.callback(clearFile.get()))
        clearFileRadioYes.pack(anchor="w")
        clearFileRadioNo = tk.Radiobutton(self, text="no", value=False, var=clearFile,
                                          command=lambda: self.callback(clearFile.get()))
        clearFileRadioNo.pack(anchor="w")

        urlSubmitButton = tk.Button(self, text="Submit",
                                    command=lambda: self.pingURL(urlInputBox.get(),
                                                                 clearFile.get()))
        urlSubmitButton.pack(side=tk.RIGHT, anchor="e")

    def callback(self, clearFile):
        print(clearFile) # Debugging Mode - check Radio box Var.

    def pingURL(self, host, clearFile):

        global r

        file = fr'c:/users/{os.getlogin()}/Desktop/ping.txt'
        text = tk.Text(self, height=5, width=100, wrap=tk.WORD)
        text.pack(side=tk.TOP, expand=True)
        if clearFile == True:

            with open(file, 'a') as output:
                output.truncate(0)
                sub.call(['ping', f'{host}'], stdout=output)

        else:

            with open(file, 'a') as output:
                sub.call(['ping', f'{host}'], stdout=output)

        output.close()

class PageOne(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="This is page 1", bg="red")
        label.pack(side="top", fill="x", pady=10)
        button = tk.Button(self, text="Go to page 2",
                           command=lambda: controller.show_frame(2))
        button.pack()

class PageTwo(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="This is page 2", bg="blue")
        label.pack(side="top", fill="x", pady=10)
        button = tk.Button(self, text="Go to the start page",
                           command=lambda: controller.show_frame(1))
        button.pack()


if __name__ == "__main__":
    app = GUI()
    app.mainloop()

点击 "提交 "后:

After clicking Submit

python user-interface tkinter python-multiprocessing python-multithreading
1个回答
1
投票

首先,你要让输出框架可用,否则以后无法访问。

self.output = tk.LabelFrame(self, text="Output", height=350, width=70)
self.output.pack(side=tk.BOTTOM, fill="both", expand=True)

然后,在... testPing() 阶层 pingURL() 方法,您可以将 Text() widget在输出标签框架中。

text = tk.Text(self.controller.output, height=5, width=100, wrap=tk.WORD)

我不知道如何使用线程或多进程。一般来说,我认为如果你针对你的每个问题发布问题,而不是在同一个问题中列出问题,你会得到更好的回应。

© www.soinside.com 2019 - 2024. All rights reserved.