最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【记录】折腾Python中的Tkinter

Python GUI crifan 7632浏览 0评论

从oschina看到了关于Python的Tkinter简介:

Tk图形用户界面 Tkinter

又从Python官网文档:

Tkinter — Python interface to Tcl/Tk

中,知道了Tkinter是Python内置的。

打算去折腾试试。


1.参考官网的代码,写了下面的:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
-------------------------------------------------------------------------------
Function:
【记录】折腾Python中的Tkinter
 
Author:     Crifan
Verison:    2012-11-30
-------------------------------------------------------------------------------
"""
 
from Tkinter import *;
 
class Application(Frame):
    def say_hi(self):
        print "hi there, everyone!"
 
    def createWidgets(self):
        self.QUIT = Button(self)
        self.QUIT["text"] = "QUIT"
        self.QUIT["fg"]   = "red"
        self.QUIT["command"] =  self.quit
 
        self.QUIT.pack({"side": "left"})
 
        self.hi_there = Button(self)
        self.hi_there["text"] = "Hello",
        self.hi_there["command"] = self.say_hi
 
        self.hi_there.pack({"side": "left"})
 
    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.pack()
        self.createWidgets()
 
def tkinterDemo():
    root = Tk()
    app = Application(master=root)
    app.mainloop()
    root.destroy()
 
     
###############################################################################
if __name__=="__main__":
    tkinterDemo();

然后去cmd中运行,可以看到有对应的图形界面显示出来了:

show gui

然后点击Hello,也可以在cmd中显示出对应的信息:

click hello show hello

还是有点意思的。

 

【总结】

算是内置的图形库,有空可以好好折腾折腾。

转载请注明:在路上 » 【记录】折腾Python中的Tkinter

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

网友最新评论 (2)

  1. 用新的ttk,是带主题的,看起来样式和原生的差不多
    melody12年前 (2012-12-01)回复
    • 好的,有空再去试试。
      crifan12年前 (2012-12-01)回复
96 queries in 0.210 seconds, using 22.01MB memory