Tkinter

 

 

目錄

  • PhotoImage

 


Mini Example

 

  1. Import the Tkinter module.
  2. Create the GUI application main window.
  3. Add one or more of the above-mentioned widgets to the GUI application.
  4. Enter the main event loop to take action against each event triggered by the user.

Code:

from Tkinter import *
root = Tk()
some = Label(root, text="Tk's job!!", width="30", height="5")
some.pack()
root.mainloop()

 


GUI 元件

 

  • Frame
  • Label
  • Button
  • Entry                         可以用來輸入東西的的欄
  • CheckButton
  • RadioButton
  • ComboBox
  • Listbox
  • Scrollbar
  • Text                          The Text widget is used to display text in multiple lines.
  • Message                    The Message widget is used to display multiline text fields for accepting values from a user

 


Standard attributes:

 

  • Dimensions
  • Colors
  • Fonts
  • Anchors
  • Relief styles
  • Bitmaps
  • Cursors

 


Geometry Management:

 

  • pack()
  • grid()
  • place

 

widget.pack( pack_options )

expand: When set to true, widget expands to fill any space not otherwise used in widget's parent.
fill: Determines whether widget fills any extra space allocated to it by the packer, or keeps its own minimal dimensions: NONE (default), X (fill only horizontally), Y (fill only vertically), or BOTH (fill both horizontally and vertically).
side: Determines which side of the parent widget packs against: TOP (default), BOTTOM, LEFT, or RIGHT.

Code:

from Tkinter import *

root = Tk()
frame = Frame(root)
frame.pack()

bottomframe = Frame(root)
bottomframe.pack( side = BOTTOM )

redbutton = Button(frame, text="Red", fg="red")
redbutton.pack( side = LEFT)

greenbutton = Button(frame, text="Brown", fg="brown")
greenbutton.pack( side = LEFT )

bluebutton = Button(frame, text="Blue", fg="blue")
bluebutton.pack( side = LEFT )

blackbutton = Button(bottomframe, text="Black", fg="black")
blackbutton.pack( side = BOTTOM)

root.mainloop()

 

widget.grid( grid_options )

column : The column to put widget in; default 0 (leftmost column).

columnspan: How many columns widgetoccupies; default 1.

ipadx, ipady :How many pixels to pad widget, horizontally and vertically, inside widget's borders.

padx, pady : How many pixels to pad widget, horizontally and vertically, outside v's borders.

row: The row to put widget in; default the first row that is still empty.

rowspan : How many rowswidget occupies; default 1.

sticky : What to do if the cell is larger than widget. By default, with sticky='', widget is centered in its cell. sticky may be the string concatenation of zero or more of N, E, S, W, NE, NW, SE, and SW, compass directions indicating the sides and corners of the cell to which widget sticks.

Code

import Tkinter
root = Tkinter.Tk()
for r in range(3):
    for c in range(4):
        Tkinter.Label(root, text='R%s/C%s'%(r,c),
            borderwidth=1 ).grid(row=r,column=c)
root.mainloop()

 

widget.place( place_options )

anchor : The exact spot of widget other options refer to: may be N, E, S, W, NE, NW, SE, or SW, compass directions indicating the corners and sides of widget; default is NW (the upper left corner of widget)

bordermode : INSIDE (the default) to indicate that other options refer to the parent's inside (ignoring the parent's border); OUTSIDE otherwise.

height, width : Height and width in pixels.

relheight, relwidth : Height and width as a float between 0.0 and 1.0, as a fraction of the height and width of the parent widget.

relx, rely : Horizontal and vertical offset as a float between 0.0 and 1.0, as a fraction of the height and width of the parent widget.

x, y : Horizontal and vertical offset in pixels.

code:

from Tkinter import *
import tkMessageBox
import Tkinter

top = Tkinter.Tk()

def helloCallBack():
   tkMessageBox.showinfo( "Hello Python", "Hello World")

B = Tkinter.Button(top, text ="Hello", command = helloCallBack)

B.pack()
B.place(bordermode=OUTSIDE, height=100, width=100)
top.mainloop()

 


元件

 

w = Frame ( master, option, ... )

master: This represents the parent window.

height  - The vertical dimension of the new frame.

width  - The default width of a checkbutton is determined by the size of the displayed image or text.
            You can set this option to a number of characters and the checkbutton will always have room for that many characters.

----------------------

w = Button ( master, option=value, ... )

 

----------------------

w = Label ( master, option, ... )

opt:

image

To display a static image in the label widget, PhotoImage, BitmapImage object.

textvariable   

To slave the text displayed in a label widget to a control variable of class StringVar

text

To display one or more lines of text in a label widget,
Internal newlines ("\n") will force a line break.

 

----------------------

w = Entry( master, option, ... )

Parameters:

command

A procedure to be called every time the user changes the state of this checkbutton.

bd

the border around the indicator. Default is 2 pixels.

show="*"

輸入的內容全部變成 "*"

state ( 不用加'' )

The default is state=NORMAL,
DISABLED to gray out the control and make it unresponsive.
If the cursor is currently over the checkbutton, the state is ACTIVE.

width

The default width of a checkbutton is determined by the size of the displayed image or text.

Methods:

get()    Returns the entry's current text as a string.

delete ( first, last=None )    Deletes characters from the widget, starting with the one at index first, up to but not including the character at position last. If the second argument is omitted, only the single character at position first is deleted.

icursor ( index )    Set the insertion cursor just before the character at the given index.

index ( index )    Shift the contents of the entry so that the character at the given index is the leftmost visible character. Has no effect if the text fits entirely within the entry.

insert ( index, s )    Inserts string s before the character at the given index.

 


PhotoImage

 

class PhotoImage Found at: Tkinter

# 只支援 GIF, PPM/PGM format

方式1:

photo = PhotoImage(file="image.gif")

方式2:

photo = """
.............
"""

photo = PhotoImage(data=photo)

圖片資訊:

  • height()
  • width()

Resize:

* x,y 只可以是 integer ...

scale_w = new_width/old_width
scale_h = new_height/old_height
photoImg.zoom(scale_w, scale_h)

 


DOC

 

* http://www.tutorialspoint.com/python/python_gui_programming.htm
# http://pydoing.blogspot.hk/2012/12/Python-Basic-Concept-of-GUI.html
 

 

 

 

 

Creative Commons license icon Creative Commons license icon