zoukankan      html  css  js  c++  java
  • Python 中的 TK编程

    可爱的 Python:Python 中的 TK编程

    http://www.ibm.com/developerworks/cn/linux/sdk/python/charm-12/

    python checkbox 用法详解

    http://www.android100.org/html/201407/13/39698.html

    Tkinter GUI编程——pack

    http://blog.sina.com.cn/s/blog_4b5039210100epkl.html


    环境安装:

    sudo apt-get install python-t

     python tkinter 窗口居中对齐:

     1     from Tkinter import *
     2     def center_window(w=300, h=200):
     3         # get screen width and height
     4         ws = root.winfo_screenwidth()
     5         hs = root.winfo_screenheight()
     6         # calculate position x, y
     7         x = (ws/2) - (w/2)   
     8         y = (hs/2) - (h/2)
     9         root.geometry('%dx%d+%d+%d' % (w, h, x, y))
    10     root = Tk()
    11     center_window(500, 300)
    12     root.mainloop()

    https://www.centos.bz/2013/01/python-tkinter-center-windows/
    滚动条的使用:
     python入门 
    http://blog.chinaunix.net/uid-13231588-id-2904121.html
    
    
    http://tkinter.unpythonic.net/wiki/ScrolledFrame
    A ScrolledFrame, with several bugs. 

    python使用Tk时固定窗口大小:

    1 from Tkinter import *
    2 root=Tk()
    3 root.minsize(50,50)
    4 root.maxsize(150,150)
    5 Label(root,text="I'm lly365").pack()
    6 root.mainloop()

    python使用Tkinter做Gui如何显示用forget隐藏的控件:

    创建控件
    bt=tkinter.Button(root,text='button')
    bt.place(x=5,y=5)
    bt.place_forget()  #隐藏控件
    
    再次bt.place() 还有传入x=5,y=5 不然不会显示 
    
    http://bbs.csdn.net/topics/320137780
    
    
      1 #!/usr/bin/env python
      2 # coding:   utf-8
      3 
      4 import os
      5 import sys
      6 import string
      7 import operator
      8 import re
      9 import threading
     10 import csv
     11 
     12 import Tkinter # import the Tkinter module
     13 
     14 from time import sleep,ctime
     15 from collections import defaultdict
     16 from collections import Counter
     17 from Tkinter import *
     18 
     19 from Tkinter import *
     20 
     21 COL_COUNT=8
     22 GM_KEYS = set(
     23         vars(Tkinter.Place).keys() +
     24         vars(Tkinter.Pack).keys() +
     25         vars(Tkinter.Grid).keys()
     26         )
     27 
     28 class ScrolledFrame(object):
     29     _managed = False
     30     # XXX These could be options
     31     x_incr = 5
     32     y_incr = 5
     33 
     34     def __init__(self, master=None, **kw):
     35         self.width = kw.pop('width', 200)
     36         self.height = kw.pop('height', 200)
     37 
     38         self._canvas = Tkinter.Canvas(master, **kw)
     39         self.master = self._canvas.master
     40         self._hsb = Tkinter.Scrollbar(orient='horizontal',
     41                 command=self._canvas.xview)
     42         self._vsb = Tkinter.Scrollbar(orient='vertical',
     43                 command=self._canvas.yview)
     44         self._canvas.configure(
     45                 xscrollcommand=self._hsb.set,
     46                 yscrollcommand=self._vsb.set)
     47 
     48         self._placeholder = Tkinter.Frame(self._canvas)
     49         self._canvas.create_window(0, 0, anchor='nw', window=self._placeholder)
     50 
     51         self._placeholder.bind('<Map>', self._prepare_scroll)
     52         for widget in (self._placeholder, self._canvas):
     53             widget.bind('<Button-4>', self.scroll_up)
     54             widget.bind('<Button-5>', self.scroll_down)
     55 
     56     def __getattr__(self, attr):
     57         if attr in GM_KEYS:
     58             if not self._managed:
     59                 # Position the scrollbars now.
     60                 self._managed = True
     61                 if attr == 'pack':
     62                     self._hsb.pack(side='bottom', fill='x')
     63                     self._vsb.pack(side='right', fill='y')
     64                 elif attr == 'grid':
     65                     self._hsb.grid(row=1, column=0, sticky='ew')
     66                     self._vsb.grid(row=0, column=1, sticky='ns')
     67             return getattr(self._canvas, attr)
     68 
     69         else:
     70             return getattr(self._placeholder, attr)
     71     def yscroll(self, *args):
     72         self._canvas.yview_scroll(*args)
     73     def scroll_up(self, event=None):
     74         self.yscroll(-self.y_incr, 'units')
     75     def scroll_down(self, event=None):
     76         self.yscroll(self.y_incr, 'units')
     77     def see(self, event):
     78         widget = event.widget
     79         w_height = widget.winfo_reqheight()
     80         c_height = self._canvas.winfo_height()
     81         y_pos = widget.winfo_rooty()
     82 
     83         if (y_pos - w_height) < 0:
     84             # Widget focused is above the current view
     85             while (y_pos - w_height) < self.y_incr:
     86                 self.scroll_up()
     87                 self._canvas.update_idletasks()
     88                 y_pos = widget.winfo_rooty()
     89         elif (y_pos - w_height) > c_height:
     90             # Widget focused is below the current view
     91             while (y_pos - w_height - self.y_incr) > c_height:
     92                 self.scroll_down()
     93                 self._canvas.update_idletasks()
     94                 y_pos = widget.winfo_rooty()
     95 
     96 
     97     def _prepare_scroll(self, event):
     98         frame = self._placeholder
     99         frame.unbind('<Map>')
    100 
    101         if not frame.children:
    102             # Nothing to scroll.
    103             return
    104         for child in frame.children.itervalues():
    105             child.bind('<FocusIn>', self.see)
    106 
    107         width, height = frame.winfo_reqwidth(), frame.winfo_reqheight()
    108         self._canvas.configure(scrollregion=(0, 0, width, height),
    109                 yscrollincrement=self.y_incr, xscrollincrement=self.x_incr)
    110         self._canvas.configure(width=self.width, height=self.height)
    111 
    112 
    113 def test_04():
    114     root = Tkinter.Tk()
    115     
    116     center_window(root,850,580) 
    117     root.minsize(850,580)
    118     root.maxsize(850,580)
    119     sf = ScrolledFrame()
    120     sf.grid(row=0, column=0, sticky='nsew')
    121     sf.master.grid_columnconfigure(0, weight=1)
    122     sf.master.grid_rowconfigure(0, weight=1)
    123     
    124     for _ in range(10):
    125         lbl = Tkinter.Label(sf, text="Hi")
    126         lbl.pack()
    127         btn = Tkinter.Button(sf, text="Buh")
    128         btn.pack()
    129         entry = Tkinter.Entry(sf)
    130         entry.pack()
    131     
    132     root.mainloop()
    133 
    134 def test_05():
    135     root = Tkinter.Tk() # create a root window
    136     root.title('demux and protocol of the ffmpeg config')
    137     states = []      
    138     #root['width']=1000
    139     #root['height']=500
    140     center_window(root,850,580) 
    141     root.minsize(850,580)
    142     root.maxsize(850,580)
    143     sf = ScrolledFrame()
    144     sf.grid(row=0, column=0, sticky='nsew')
    145     sf.master.grid_columnconfigure(0, weight=1)
    146     sf.master.grid_rowconfigure(0, weight=1)
    147     Label(sf,text = 'please select demux type:').place(x=5,y=10)
    148     #frame = Frame(root, width=500, height=400, bd=1)
    149     #frame.pack()
    150     #iframe1 = Frame(frame, bd=2, relief=SUNKEN)
    151     chk_demux_list=[]
    152     for i in range(10):
    153         var = IntVar()
    154         chk = Checkbutton(sf, text=str(i), variable=var)
    155         if i+1<=COL_COUNT:
    156             chk.place(x=i*100+30,y=10+30*(i/COL_COUNT+1))
    157         else:
    158             chk.place(x=(i%COL_COUNT)*100+30,y=10+30*(i/COL_COUNT+1))
    159         #chk.pack(side=LEFT)
    160         states.append(var)
    161         chk_demux_list.append(chk)
    162     #Label(root,text = 'please select protocol type:').place(x=5,y=30+chk_demux_list[len(chk_demux_list)-1].winfo_y())
    163     Label(sf,text = 'please select protocol type:').place(x=5,y=250)
    164     root.mainloop() # create an event loop
    165     #print states
    166     #for var in states:
    167     #    print var.get()
    168     #print map((lambda var: var.get()), states) 
    169 def center_window(root,w=300, h=200):
    170     # get screen width and height
    171     ws = root.winfo_screenwidth()
    172     hs = root.winfo_screenheight()
    173     # calculate position x, y
    174     x = (ws/2) - (w/2)   
    175     y = (hs/2) - (h/2)
    176     #80x80代表了初始化时主窗口的大小,0,0代表了初始化时窗口所在的位置
    177     root.geometry('%dx%d+%d+%d' % (w, h, x, y))
    178 def test_02():
    179     root = Tkinter.Tk() # create a root window
    180     root.title('demux and protocol of the ffmpeg config')
    181     states = []      
    182     #root['width']=1000
    183     #root['height']=500
    184     Label(root,text = 'please select demux type:').place(x=5,y=10)
    185     center_window(root,850,580) 
    186     root.minsize(850,580)
    187     root.maxsize(850,580)
    188     #frame = Frame(root, width=500, height=400, bd=1)
    189     #frame.pack()
    190     #iframe1 = Frame(frame, bd=2, relief=SUNKEN)
    191     chk_demux_list=[]
    192     for i in range(100):
    193         var = IntVar()
    194         chk = Checkbutton(root, text=str(i), variable=var)
    195         if i+1<=COL_COUNT:
    196             chk.place(x=i*100+30,y=10+30*(i/COL_COUNT+1))
    197         else:
    198             chk.place(x=(i%COL_COUNT)*100+30,y=10+30*(i/COL_COUNT+1))
    199         #chk.pack(side=LEFT)
    200         states.append(var)
    201         chk_demux_list.append(chk)
    202     #Label(root,text = 'please select protocol type:').place(x=5,y=30+chk_demux_list[len(chk_demux_list)-1].winfo_y())
    203     Label(root,text = 'please select protocol type:').place(x=5,y=250)
    204     root.mainloop() # create an event loop
    205     #print states
    206     #for var in states:
    207     #    print var.get()
    208     #print map((lambda var: var.get()), states) 
    209 
    210 
    211 
    212 
    213 def test_03():
    214     root = Tk()
    215     root.title("Note Taker")
    216     def Button1():
    217         listbox.insert(END, "button1 pressed")
    218     def Button2():
    219         listbox.insert(END, "button2 pressed")
    220     def Button3():
    221         text_contents = text.get()
    222         listbox.insert(END, text_contents)
    223         text.delete(0,END)
    224     button1 = Button(root, text="button1", command = Button1)
    225     button2 = Button(root, text="button2", command = Button2)
    226     button3 = Button(root, text="button3", command = Button3)
    227     text = Entry(root)
    228     scrollbar = Scrollbar(root, orient=VERTICAL)
    229     listbox = Listbox(root, yscrollcommand=scrollbar.set)
    230     scrollbar.configure(command=listbox.yview)
    231     text.pack()
    232     button1.pack()
    233     button2.pack()
    234     button3.pack()
    235     listbox.pack()
    236     scrollbar.pack()
    237     root.mainloop() 
    238 
    239 def test_01():
    240     #content  ==>  ###pos=350143600,pts=2676718###
    241     #filename="F:\yingc\work\goxceed-dvbs-hd\6605\solution\aa"
    242     filename="./aa"
    243     pos=-1
    244     dts=-1
    245     poslist=[]
    246     dtslist=[]
    247     
    248     str1="###pos="
    249     str2=",pts="
    250     
    251     f = open(filename)
    252     for line in f:
    253         aa=line[0:len(str1)]
    254         if aa == str1:
    255             pos=line[len(str1):line.index(str2)]
    256             dts=line[line.index(str2)+len(str2):len(line)-3-1]
    257             poslist.append(pos)
    258             dtslist.append(dts)
    259     f.close()
    260     
    261     #s=[11,22,11,44,22,33]
    262     d = defaultdict(list)
    263     for k,va in [(v,i) for i,v in enumerate(poslist)]:
    264         d[k].append(va)
    265     #print  d.items()
    266     count=0
    267     for value in d.items():
    268         if len(value[1])>1:
    269             print value
    270             count=count+1
    271     print "poslen:"+str(len(poslist))+",dtslen"+str(len(dtslist))
    272     print str(len(d))+","+str(count)
    273 
    274     #d = defaultdict(list)
    275     #for k,va in [(v,i) for i,v in enumerate(dtslist)]:
    276     #    d[k].append(va)
    277     ##print  d.items()
    278     #for value in d.items():
    279     #    if len(value[1])>4:
    280     #        print value
    281 
    282     ##print Counter([11,22,11,44,22,33])
    283 
    284 
    285 
    286 
    287 if __name__ == "__main__":
    288     test_02()
    289     print "finish"
    290 
    291     
    292     
    293     
    294 
    295     
    296 
    297 
    298     
    299     
    300     
    301 
    302     
    Python:tkinter-Parent获取弹出窗口的返回值
    http://blog.sina.com.cn/s/blog_ac9fdc0b0101naol.html


    Tkinter里没有messagebox,
    在Python2.5里,要从 tkMessageBox导入
    from tkMessageBox import * 然后直接用showinfo()来实现输出 showinfo(message=“hello”) 这个时候不需要使用任何窗口路径来调用它,直接调用它就可以了。






    aa
  • 相关阅读:
    JDBC_查询
    微信web小程序开发1
    小型人口普查系统2
    小型人口普查系统1
    JSP内置对象作用域
    Session和Cookie的区别
    Cookie
    Session
    c++中嵌入python
    目标文件obj的各段 2
  • 原文地址:https://www.cnblogs.com/jingzhishen/p/3860788.html
Copyright © 2011-2022 走看看