python - 如何使用 Python tkinter 在另一个框架的文本框中显示过程输出

标签 python user-interface tkinter python-multiprocessing python-multithreading

<分区>

我正在尝试将“ping”输出写入一个文本框 在“输出”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

最佳答案

首先,您必须使输出帧可用,否则您以后无法访问它:

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

然后,在 testPing()pingURL() 方法中,您可以将 Text() 小部件打包到输出标签框中:

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

我不知道使用线程或多进程。总的来说,我认为如果您针对每个问题发布问题而不是将它们列在同一个问题中,您会得到更好的答复。

关于python - 如何使用 Python tkinter 在另一个框架的文本框中显示过程输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62124711/

相关文章:

python - 强制python打印一定数量的小数位

python - (n) python 中的 curses pad 不工作

python - tkinter标签图像固定位置

python - GAE Python : Webapp2 Equivalent of Flask request. 数据

android - 在后台运行应用程序

c++ - 如何设置 QPushButton 的精确大小?

c# - 等同于 C# 中的 Swing 包

javascript - 如何在 React 中使用复选框管理状态?

Python tkinter 弹跳球 - 能量趋于无穷大

python - 如何检测 OptionMenu 或 Checkbutton 何时更改?