丁克是一个蟒蛇替代Tkinter。
teek的Python项目详细描述
三通
teek是一个pythonic和用户友好的tkinter替代品。不会来的 有了python,您需要自己安装它,但它又好又轻。
文档:https://teek.rtfd.org/" rel="nofollow">https://teek.rtfd.org/
丁克是蟒蛇
如果你和Tkinter合作过很多次,你知道这有点烦人。 在tcl中,几乎所有内容都表示为字符串。Tkinter是个笨蛋 不会像通常用python那样做;相反, Tkinter用户需要自己处理许多不便之处。在另一个 手,teek是蟒蛇的;它做的事情就像在python中做得最好一样, 而不是在tcl中是如何做到的。
Ttk
从没听说过TTK?你真丢脸。ttk是编写gui的新方法 TK,你应该已经在Tkinter里用过了。TTK图形用户界面看起来好多了 比大多数平台上的非ttk gui。例如,这个gui有一个ttk按钮和 非TTK按钮。猜猜哪个:
问题是TK的窗口(在Tkinter中,Tkinter.toplevel
和
tkinter.tk
)不是ttk小部件。如果将ttk小部件添加到其中,则gui
在某些系统上看起来很混乱,比如我的Linux系统和Mate桌面。这个
解决方案是添加一个填充窗口的大ttk框架,然后添加所有
小部件放入该框架。
tkinter:
# this is a well-done hello world, and tbh, most people don't use tkinter "well"importtkinterfromtkinterimportttkroot=tkinter.Tk()big_frame=ttk.Frame(root)big_frame.pack(fill='both',expand=True)# make sure it fills the root windowttk.Label(big_frame,text="Hello World!").pack()root.mainloop()
发球台:
importteekwindow=teek.Window("Hello")teek.Label(window,"Hello World!").pack()window.on_delete_window.connect(teek.quit)teek.run()
所有teek小部件都是ttk,因此不需要单独导入即可使用
TTK小部件。另外,当您创建一个teek窗口时,大的ttk框架是
为您自动创建和打包,无需考虑
您只需创建一个
窗口并在其中添加内容即可。
螺纹
这里time.sleep
s表示阻塞。在现实生活中你可以
网络请求、运行子进程或在
线程,
tkinter:
importqueueimportthreadingimporttimeimporttkinterroot=tkinter.Tk()root.title("Thread Demo")text=tkinter.Text(root)text.pack()message_queue=queue.Queue()defqueue_poller():whileTrue:try:message=message_queue.get(block=False)exceptqueue.Empty:breaktext.insert('end',message)root.after(50,queue_poller)defthread_target():message_queue.put('doing things...\n')time.sleep(1)message_queue.put('doing more things...\n')time.sleep(2)message_queue.put('done')threading.Thread(target=thread_target).start()queue_poller()root.mainloop()
发球台:
importthreadingimporttimeimportteektext=teek.Text(teek.Window("Thread Demo"))text.pack()defthread_target():text.insert(text.end,'doing things...\n')time.sleep(1)text.insert(text.end,'doing more things...\n')time.sleep(2)text.insert(text.end,'done')teek.init_threads()threading.Thread(target=thread_target).start()window.on_delete_window.connect(teek.quit)teek.run()
这不是玩笑。使用Tkinter线程是一个可怕的混乱,但是Teek
很好地处理线程。您只需要teek.init_threads()
,然后
能用线做很多事情。有关详细信息,请参见并发文档。
可调试性
tkinter:
label=ttk.Label(some_widget,text="hello world")print(label)# prints something like '.140269016152776', which is confusingprint(repr(label))# somewhat better: <tkinter.ttk.Label object .140269016152776>
发球台:
label=teek.Label(some_widget,"hello world")print(label)# <teek.Label widget: text='hello world'>
文本小部件索引
文本小部件第3行的第4个字符是
TCL和Tkinter。这不仅令人困惑,因为3.4
看起来像一个浮点数
尽管把它当作浮点数会把事情搞砸,但这会使代码看起来
乱七八糟。
tkinter:。
# figure out where the cursor isline,column=textwidget.index('insert').split('.')line=int(line)column=int(column)# same thing, more conciseline,column=map(int,textwidget.index('insert').split('.'))
发球台:
line,column=textwidget.marks['insert']
textwidget.marks
是一个类似字典的对象,将名称标记为键和文本
索引namedtuples作为值。teek将文本索引表示为namedtuples
具有line
和column
属性,如果您只需要
线。在tkinter中,需要用
.split('.')
并获取拆分结果的第一个元素。
tkinter:
cursor_lineno=int(textwidget.index('insert').split('.')[0])
发球台:
# this is a well-done hello world, and tbh, most people don't use tkinter "well"importtkinterfromtkinterimportttkroot=tkinter.Tk()big_frame=ttk.Frame(root)big_frame.pack(fill='both',expand=True)# make sure it fills the root windowttk.Label(big_frame,text="Hello World!").pack()root.mainloop()0
在tkinter中,您还需要自己构造'line.column'
字符串,但是
在teek中,您可以使用(行,列)
元组。
tkinter:
# this is a well-done hello world, and tbh, most people don't use tkinter "well"importtkinterfromtkinterimportttkroot=tkinter.Tk()big_frame=ttk.Frame(root)big_frame.pack(fill='both',expand=True)# make sure it fills the root windowttk.Label(big_frame,text="Hello World!").pack()root.mainloop()1
发球台:
# this is a well-done hello world, and tbh, most people don't use tkinter "well"importtkinterfromtkinterimportttkroot=tkinter.Tk()big_frame=ttk.Frame(root)big_frame.pack(fill='both',expand=True)# make sure it fills the root windowttk.Label(big_frame,text="Hello World!").pack()root.mainloop()2
tcl使用类似3.4+5个字符的字符串来表示5的位置。
位置后的字符
3.4
。teek的文本位置名为duples有一个
pythonicforward()
返回新文本位置的方法。
tkinter:
# this is a well-done hello world, and tbh, most people don't use tkinter "well"importtkinterfromtkinterimportttkroot=tkinter.Tk()big_frame=ttk.Frame(root)big_frame.pack(fill='both',expand=True)# make sure it fills the root windowttk.Label(big_frame,text="Hello World!").pack()root.mainloop()3
发球台:
# this is a well-done hello world, and tbh, most people don't use tkinter "well"importtkinterfromtkinterimportttkroot=tkinter.Tk()big_frame=ttk.Frame(root)big_frame.pack(fill='both',expand=True)# make sure it fills the root windowttk.Label(big_frame,text="Hello World!").pack()root.mainloop()4
超时
在Tkinter中,任何一个小部件。在(1000,func)
之后,在1秒之后运行func()
,并且
any_小部件
可以是任何tkinter小部件。对,你需要一个小部件
安排超时。这可能是库代码中的问题。但是如果在
一秒钟的等待时间,你决定你不想运行
毕竟超时了?你可以取消暂停,但和往常一样,蒂克做到了
更容易。
tkinter:
# this is a well-done hello world, and tbh, most people don't use tkinter "well"importtkinterfromtkinterimportttkroot=tkinter.Tk()big_frame=ttk.Frame(root)big_frame.pack(fill='both',expand=True)# make sure it fills the root windowttk.Label(big_frame,text="Hello World!").pack()root.mainloop()5
发球台:
# this is a well-done hello world, and tbh, most people don't use tkinter "well"importtkinterfromtkinterimportttkroot=tkinter.Tk()big_frame=ttk.Frame(root)big_frame.pack(fill='both',expand=True)# make sure it fills the root windowttk.Label(big_frame,text="Hello World!").pack()root.mainloop()6
开发teek
本节包含我在处理teek时使用的命令。如果你 使用windows,将python3替换为py
python3-m pip安装--用户sphinx pytest pytest cov flit
安装 开发Teek所需的一切。python3-m pytest
运行测试(它们位于tests
子目录中)。它是 正常情况下,运行测试时屏幕上会出现很多小窗口。我 使用以下pytest选项:--skipslow
跳过 装饰有@pytest.mark.slow
--durations=10
在 试运行。这是一个很好的方法来确定哪些测试标记为慢速。--cov=teek
在覆盖范围内运行测试。跑python3-m coverage html
并打开htmlcov/index.html
查看 结果。travis构建的覆盖范围结果转到工作服
cd docs
然后是py-m sphinx。_在本地生成文档。 您可以通过在浏览器中打开
docs/u build/index.html
来查看它。 当您向master推送文档时,readthedocs会生成文档,但最好 首先要确保一切正常。- sphinx似乎只构建文档的一部分,如果您更改了
它,但有时它检测不到你的变化。运行CD文档
rm-r\u构建
使其下次构建所有内容。 - 我通常不在我的系统上整理文件。我推到github(任何
如果travis构建失败,我知道我做得很糟糕。如果
你想自己整理东西,找到正确的命令
.travis.yml
flit publish
上传到pypi。你可以让我在做完后再做这个 将某些内容合并到母版。