[英]How to return string value of Console output to put it into a label in Tkinter
I made a python script that has a lot of print statements showing what the program is doing rather than just having it just sit there and the python script works fine now I am creating a front end with tkinter.我制作了一个 python 脚本,其中有很多打印语句显示程序正在做什么,而不仅仅是让它坐在那里,并且 python 脚本工作正常,现在我正在使用 tkinter 创建前端。 What I used to send the print statements to return them is some thing like this:
我用来发送打印语句以返回它们是这样的:
test.py
def PrintX():
X = [1,2,3,4,5]
for x in X:
print(x)
My plan is to have a tkinter frame in that I put a label and set the text variable to my function in my script.我的计划是在我的脚本中放置一个 label 并将文本变量设置为我的 function 有一个 tkinter 框架。 My tkinter page script looks like this so far:
到目前为止,我的 tkinter 页面脚本如下所示:
class TradingBotapp(tk.Tk):
def __init__(self,*args,**kwargs):
tk.Tk.__init__(self,*args,**kwargs)
container = tk.Frame(self)
container.pack(side='top',fill='both',expand= True)
container.grid_rowconfigure(0,weight = 1)
container.grid_columnconfigure(0,weight = 1)
self.frames = {}
for F in (InitalSetup):
frame = F(container,self)
self.frames[F] = frame
frame.grid(row=0,column=0,sticky='nsew')
self.show_frame(InitalSetup)
def show_frame(self,cont):
frame = self.frames[cont]
frame.tkraise()
class InitalSetup(tk.Frame):
def __init__(self,parent,controller):
tk.Frame.__init__(self,parent)
label = tk.Label(self, text='Setup', font = LARGE_FONT).pack()
Frame = tk.Frame(self,width=768,height=576).pack()
lbl = tk.Message(Frame, text='').pack()
button1 = ttk.Button(self, text='Start Setup',command=lambda:callback2(lbl)).pack()
def callback2(object):
old_stdout = sys.stdout
sys.stdout = StdoutRedirectorLabel(lbl)
setup()
#lbl['text'] = sys.stdout.result.strip()
sys.stdout = old_stdout
class StdoutRedirectorLabel(object):
def __init__(self,widget):
self.widget = widget
self.widget['text'] = ''
def write(self, text):
self.widget['text'] += text
app = TradingBotapp()
app.mainloop()
But nothing is showing up, but when I press the button I get self.widget['text'] = '' TypeError: 'NoneType' object does not support item assignment
any help would be much appreciated但是什么都没有出现,但是当我按下按钮时,我得到
self.widget['text'] = '' TypeError: 'NoneType' object does not support item assignment
任何帮助将不胜感激
Haha... You've fallen victim to one of the classic blunders,, (In all seriousness, I've done this many times before, you're not alone) I believe you need to pack your labels with label1.pack()
after their creation.哈哈...你已经成为经典错误之一的受害者,(说真的,我以前做过很多次,你并不孤单)我相信你需要用
label1.pack()
创建后。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.