Python 2.7,我希望毫不拖延地实时打印消息。即当我打印消息时,它会调用函数Mainprintf
,但它会在文本字段中一次打印所有消息。但我希望它应该在Text小部件中逐个打印,以便我可以看到它正在打印。
注意:一个接一个的打印只能在终端中看到 这个问题在任何论坛都没有完全回答。有谁能建议以下查询? 示例代码如下:
from Tkinter import *
import time
import os
import sys
Gray = 'Gray'
def UserInput():
global UI_MainForm
UI_MainForm = Tk()
UI_MainForm.resizable(0,0)
UI_MainForm.geometry("350x450+500+25") #Wight X height + X - Position + Y - Position
UI_MainForm.title("Main Menu")
UI_MainForm.deiconify()
labelframe = LabelFrame(UI_MainForm,text="Please select the below options:",width=300, height=100,bd = 2)
labelframe.pack(fill="both")
labelframe.config(relief=RIDGE)
labelframe1 = LabelFrame(UI_MainForm,text="User Input:'Options'",width=300, height=100,bd = 2)
labelframe1.pack(pady=8,fill="both")
labelframe1.config(relief=FLAT)
#Frame to add the Scrollbar and Text box
frame = Frame(UI_MainForm,width=200, height=250)
frame.pack(fill = 'both',padx=3, pady=15)
frame.config(relief=FLAT)
global entrytext
global entry
entrytext = StringVar()
entry = Entry(labelframe1,textvariable=entrytext,width=35)
entry.pack(padx = 1, pady = 5)
Input = entrytext.get()
#Main Menu Label Display items
MainMenuDisplay = [{"ID": 1,"Description": "Display1"},
{"ID": 2,"Description": "Display2"}]
for menu in MainMenuDisplay:
temp_text = '{0}. {1}'.format(menu['ID'], menu['Description'])
Label(labelframe, text=temp_text).pack(anchor = W)
ButtonOK = Button(labelframe1, text = "OK", command =EventButtonOK, width =15)
ButtonOK.pack(side = LEFT, padx = 15, pady = 15)
UI_MainForm.bind('<Return>', EventButtonOK)
ButtonEXIT = Button(labelframe1, text = "EXIT", command =EventButtonExist, width =15)
ButtonEXIT.pack(side = RIGHT, padx = 15)
UI_MainForm.bind('<Escape>', EventButtonExist)
global Main_UI_SB
global Main_UI_TxtBar
Main_UI_SB = Scrollbar(frame)
Main_UI_SB.pack(side=RIGHT, fill=Y)
Main_UI_TxtBar = Text(frame, height=30, width=45,wrap=WORD)
Main_UI_TxtBar.pack(side=LEFT, fill=Y)
Main_UI_SB.config(command=Main_UI_TxtBar.yview)
Main_UI_TxtBar.config(yscrollcommand=Main_UI_SB.set)
Main_UI_TxtBar.tag_configure('Red', foreground = 'red')
Main_UI_TxtBar.tag_configure('Black', foreground = 'black')
Main_UI_TxtBar.tag_configure('Green', foreground = '#005A2B')
Main_UI_TxtBar.tag_configure('Gray', foreground = 'grey')
UI_MainForm.mainloop()
return;
UserInput()
def EventButtonExist(event=None):
UI_MainForm.destroy()
def EventButtonOK(event=None):
entry.selection_range(0, END)
Main_UI_TxtBar.delete(1.0,END)
# Input = int(entrytext.get())
Input = MainTakeStrInt()
if (Input == 1):
display1();
elif (Input == 2):
display2();
else:
Mainprintf('The Input is not valid',Red)
return;
def MainTakeStrInt():
cast_value = (entrytext.get())
try:
cast_test = int(cast_value)
return cast_test
except ValueError:
return cast_value
我的显示信息,调用函数Mainprintf
def display2():
Main_UI_TxtBar.delete(1.0,END)
entry.selection_range(0, END)
Mainprintf('\nEntering Message1', Gray)
time.sleep(2)
Mainprintf('\nEntering Message2', Gray)
time.sleep(2)
Mainprintf('\nEntering Message3', Gray)
return;
def display1():
Mainprintf('\nEntering Message1', Gray)
time.sleep(2)
Mainprintf('\nEntering Message2', Gray)
time.sleep(2)
Mainprintf('\nEntering Message3', Gray)
return;
Mainprintf功能:
def Mainprintf(Message, Color):
global Main_UI_SB
global Main_UI_TxtBar
Main_UI_TxtBar.insert(END, Message, Color)
Main_UI_TxtBar.see("end")
print Message
return;