Python 如果显示其他文本,则终止原始输入()
我有一个简单的服务器应用程序,我用Python使用SocketServer制作,它有一个非常原始的命令行类型输入系统。我这里的主要问题是,当服务器收到消息时,它会将消息打印到屏幕上。这一切都很好,除了raw_输入函数仍在等待输入和检查文本。在server handle()函数中,是否有方法停止原始输入或引发一些异常以结束输入并显示服务器正在接收的信息 谢谢,Python 如果显示其他文本,则终止原始输入(),python,raw-input,Python,Raw Input,我有一个简单的服务器应用程序,我用Python使用SocketServer制作,它有一个非常原始的命令行类型输入系统。我这里的主要问题是,当服务器收到消息时,它会将消息打印到屏幕上。这一切都很好,除了raw_输入函数仍在等待输入和检查文本。在server handle()函数中,是否有方法停止原始输入或引发一些异常以结束输入并显示服务器正在接收的信息 谢谢, Zack。据我所知,这是不可能的,因为原始输入接受来自python命令控制台的输入。但是,有一些方法可以避免这种情况,这可能是意料之外的:
Zack。据我所知,这是不可能的,因为原始输入接受来自python命令控制台的输入。但是,有一些方法可以避免这种情况,这可能是意料之外的: 1-不使用控制台,而是创建一个带有输出和输入行的简单Tkinter窗口。创建一个自定义打印函数,将消息附加到窗口文本的末尾(可以是使用固定宽度字体的滚动条),然后创建一个响应enter的命令提示框。其代码如下所示:
from Tkinter import *
root = Tk()
topframe=Frame(root)
bottomframe=Frame(root)
bottomframe.pack(side=BOTTOM,fill=X)
topframe.pack(side=TOP,fill=BOTH)
scrollbar = Scrollbar(topframe)
scrollbar.pack(side=RIGHT,fill=Y)
text = Text(topframe,yscrollcommand=scrollbar.set)
text.pack(side=LEFT,fill=BOTH)
scrollbar.config(command=text.yview)
text.config(state=DISABLED)
v = StringVar()
e = Entry(bottomframe,textvariable=v)
def submit():
command = v.get()
v.set('')
#your input handling code goes here.
wprint(command)
#end your input handling
e.bind('<Return>',submit)
button=Button(bottomframe,text='RUN',command=submit)
button.pack(side=RIGHT)
e.pack(expand=True,side=LEFT,fill=X)
def wprint(obj):
text.config(state=NORMAL)
text.insert(END,str(obj)+'\n')
text.config(state=DISABLED)
root.mainloop()
import threading
wlock=threading.Lock()
printqueue=[]
rinput=False
def winput(text):
with wlock:
global printqueue,rinput
rinput=True
text = raw_input(text)
rinput=False
for text in printqueue:
print(text)
printqueue=[]
return text
def wprint(obj):
global printqueue
if not(rinput):
print(str(obj))
else:
printqueue.append(str(obj))
据我所知,这是不可能的,因为raw_输入接受来自python命令控制台的输入。但是,有一些方法可以避免这种情况,这可能是意料之外的: 1-不使用控制台,而是创建一个带有输出和输入行的简单Tkinter窗口。创建一个自定义打印函数,将消息附加到窗口文本的末尾(可以是使用固定宽度字体的滚动条),然后创建一个响应enter的命令提示框。其代码如下所示:
from Tkinter import *
root = Tk()
topframe=Frame(root)
bottomframe=Frame(root)
bottomframe.pack(side=BOTTOM,fill=X)
topframe.pack(side=TOP,fill=BOTH)
scrollbar = Scrollbar(topframe)
scrollbar.pack(side=RIGHT,fill=Y)
text = Text(topframe,yscrollcommand=scrollbar.set)
text.pack(side=LEFT,fill=BOTH)
scrollbar.config(command=text.yview)
text.config(state=DISABLED)
v = StringVar()
e = Entry(bottomframe,textvariable=v)
def submit():
command = v.get()
v.set('')
#your input handling code goes here.
wprint(command)
#end your input handling
e.bind('<Return>',submit)
button=Button(bottomframe,text='RUN',command=submit)
button.pack(side=RIGHT)
e.pack(expand=True,side=LEFT,fill=X)
def wprint(obj):
text.config(state=NORMAL)
text.insert(END,str(obj)+'\n')
text.config(state=DISABLED)
root.mainloop()
import threading
wlock=threading.Lock()
printqueue=[]
rinput=False
def winput(text):
with wlock:
global printqueue,rinput
rinput=True
text = raw_input(text)
rinput=False
for text in printqueue:
print(text)
printqueue=[]
return text
def wprint(obj):
global printqueue
if not(rinput):
print(str(obj))
else:
printqueue.append(str(obj))
你为什么不为这类事情使用GUI框架?你为什么不为这类事情使用GUI框架?