CLI + GUI [英] CLI+GUI

查看:73
本文介绍了CLI + GUI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道使用Tkinter

以及面向命令行的应用程序的推荐方法是什么。

我想到的是这样的:


导入cmd,sys


类CMD(cmd.Cmd):

def do_this(self,* args):

draw_a_diagram()

def do_that(self,* args):

draw_another_diagram()

def do_exit(self, * args):

sys.exit(0)

c = CMD()

c.cmdloop()


这里的方法``draw_a_diagram``和``draw_another_diagram``

应该按照你的预期进行。请注意,我不想使用

不同的窗口,图表应该显示在另一个窗口的相同

窗口上。顺便说一句,我并没有真正展示

图表,这只是我能想出的最简单的例子

由命令行解释器驱动的图形程序。

我想继续使用CLI界面,我非常喜欢

,这得益于readline和完成支持,简单的使用和简单的实现。


我不知道如何做到这一点,除了通过扭曲

,比如在文件中保存(表示)图表
并且有一个单独的Tkinter进程,定期查看

文件,如果文件已更新则更改显示。我认为有更好的方法,因为这是一个很常见的问题。

有什么建议吗?


TIA,


Michele

I wonder what is the recommended way of using Tkinter
together with a command line oriented application.
I have in mind something like that:

import cmd,sys

class CMD(cmd.Cmd):
def do_this(self,*args):
draw_a_diagram()
def do_that(self,*args):
draw_another_diagram()
def do_exit(self,*args):
sys.exit(0)

c=CMD()
c.cmdloop()

Here the methods ``draw_a_diagram`` and ``draw_another_diagram``
should do what you expect. Notice that I don''t want to use
different windows, the graphs should be displayed on the same
window one over the other. BTW, I am not really displaying
diagrams, this is only the simplest example I can figure out
of graphics program driven by a command line interpreter.
I want to stay with the CLI interface, which I like a lot
thanks to the readline and completion support, the easy
of use and the easy of implementation.

I have no idea of how to do that, except via contorsions
such as saving (a representation of) the diagrams in a file
and having a separated Tkinter process that periodically look at the
file, changing the display if the file has been updated. I
guess there is a better way, since this is a quite common issue.
Any suggestion?

TIA,

Michele

推荐答案

mi **@pitt.edu (Michele Simionato)在留言中写道:< 22 ************************** @ posted 。谷歌。 com> ...
mi**@pitt.edu (Michele Simionato) wrote in message news:<22**************************@posting.google. com>...
我想知道将Tkinter与命令行导向应用程序一起使用的推荐方法是什么。
I wonder what is the recommended way of using Tkinter
together with a command line oriented application.




回复自己...


我试图通过

线程模块实现我之前在邮件中讨论的内容:

#cmdriven.py


导入Tkinter为t

导入cmd,穿线


root = t.Tk()

s = t.StringVar()

s.set(''ciao'')

label = t。标签(root,textvariable = s)

label.pack()


class Cmd(cmd.Cmd):

def do_display(self,arg):

s.set(arg)

def do_quit(self,arg):

root.quit()

返回''退出''#anything<>没有人会做这个工作


def cmdloop(stringvar):

尝试:Cmd()。cmdloop()

最后:如果有时出错,则#gracely退出


thread = threading.Thread(target = cmdloop,args =(s,))

thread.start()

root.mainloop()


如果我做了类似的事情



Replying to myself ...

I tried to implement what I discussed in my previous mail via the
threading module:

#cmdriven.py

import Tkinter as t
import cmd,threading

root=t.Tk()
s=t.StringVar()
s.set(''ciao'')
label=t.Label(root,textvariable=s)
label.pack()

class Cmd(cmd.Cmd):
def do_display(self,arg):
s.set(arg)
def do_quit(self,arg):
root.quit()
return ''quit'' # anything <> None will do the job

def cmdloop(stringvar):
try: Cmd().cmdloop()
finally: pass # gracefully exit if sometimes goes wrong

thread=threading.Thread(target=cmdloop,args=(s,))
thread.start()
root.mainloop()

It works if I do something like


python cmdriven.py

(Cmd)显示你好

(Cmd)显示它有效!

(Cmd)退出


然而,我想知道这是否是一个强大的解决方案,如果我应该期待在更复杂的情况下出现问题
(一段时间过去了......我有

刚刚发现这个脚本在Windows 98下挂起!)


BTW,我有另一个问题,为什么Cmd类没有默认退出

方法?看看源代码我看到任何返回某些东西的方法

与None不同会停止命令循环,所以我用了这个

hack,但我不是喜欢它。也许我错过了文档中的内容?

谢谢,

Michele
python cmdriven.py
(Cmd) display hello
(Cmd) display It works!
(Cmd) quit

However, I wonder if this is a robust solution and if I should expect
problems in more complicate situations (some time passes ... I have
just discovered that this script hangs under Windows 98!)

BTW, I have another question, why the Cmd class does not have a default quit
method? Looking at the source I see that any method returning something
different from None will stop the command loop, and so I have used this
hack, but I don''t like it. Maybe I have missed something in the documentation?
Thanks,

Michele


Michele Simionato写道:
Michele Simionato wrote:
mi**@pitt.edu (Michele Simionato)在留言中写道:< 22 ** ************************@posting.google。 com> ...
mi**@pitt.edu (Michele Simionato) wrote in message news:<22**************************@posting.google. com>...
我想知道将Tkinter与面向命令行的应用程序一起使用的推荐方法是什么。
I wonder what is the recommended way of using Tkinter
together with a command line oriented application.



回复自己...

我试图通过
线程模块实现我之前在邮件中讨论过的内容:

#cmdriven.py

导入Tkinter为导入cmd,穿线

root = t.Tk()
s = t.StringVar()
s.set (''ciao'')
label = t.Label(root,textvariable = s)
label.pack()
类Cmd(cmd.Cmd):
def do_display(self,arg):
s.set(arg)
def do_quit(self,arg):
root.quit()
返回''退出' '#anything<>没有人会做这个工作

def cmdloop(stringvar):
尝试:Cmd()。cmdloop()
最后:如果有时候出错,通过#gracely退出

thread = threading.Thread(target = cmdloop,args =(s,))
thread.start()
root.mainloop()

如果有效我做了类似的事情


Replying to myself ...

I tried to implement what I discussed in my previous mail via the
threading module:

#cmdriven.py

import Tkinter as t
import cmd,threading

root=t.Tk()
s=t.StringVar()
s.set(''ciao'')
label=t.Label(root,textvariable=s)
label.pack()

class Cmd(cmd.Cmd):
def do_display(self,arg):
s.set(arg)
def do_quit(self,arg):
root.quit()
return ''quit'' # anything <> None will do the job

def cmdloop(stringvar):
try: Cmd().cmdloop()
finally: pass # gracefully exit if sometimes goes wrong

thread=threading.Thread(target=cmdloop,args=(s,))
thread.start()
root.mainloop()

It works if I do something like


这篇关于CLI + GUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆