gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 09:45:49 25 4
gpt4 key购买 nike

<分区>

我正在尝试将“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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com