python模块tkinter的常用控件1

demo
原文

1. Label

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
# .config(**options)
label = tkinter.Label(
root,
text = "this is a label text 测试",
bg = "green", # background
fg = "red", # foreground
font = ("arial",15),
width = 20,
height = 5,
activebackground = 'blue', # with the state option
activeforeground = "purple",
anchor = 'ne', # (center)where in the label the text should be located.
# bitmap =
borderwidth = 10, # bd
compound = 'top', # (center)Controls how to combine text and image in the label
# cursor =
disabledforeground = "red",
highlightbackground = "blue",
highlightcolor = "green",
highlightthickness = 2,
# image =
justify = 'center', # Defines how to align multiple lines of text ,right,left,center
padx = 10,
pady = 10,
relief = 'ridge', # (flat),sunken,raised,groove
state = 'normal', # active,disabled
# takefocus =
# textvariable =
underline = 1, # (-1)
wraplength = 100
)
label.pack(side="left")

2. Frame

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# .config(**options)
frame = tkinter.Frame(
root,
bg = "red",
width = 400,
height = 600,
# colormap =
# container =
# cursor =
# class =
bd = 2,
# highlightbackground =
# highlightcolor =
# highlightthickness =
relief = 'sunken', # flat,sunken,raised,groove,ridge
# takefocus =
# visual =
padx = 10,
pady = 10
)
frame.pack()

3. Entry

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# .config(**options)
value = tkinter.StringVar()
entry = tkinter.Entry(
root,
width = 200,
bg = "#222222",
fg = "yellow",
bd = 20,
textvariable = value,
# cursor =
# disabledbackground =
# disabledforeground =
font = ("arial",15),
exportselection = True, #selected text is automatically exported to the clipboard.
# highlightbackground =
# highlightcolor =
# highlightthickness =
insertbackground = "blue", # Color used for the insertion cursor.
insertborderwidth = 10,
insertofftime = 1000, # Together with insertontime, this option controls cursor blinking. Both values are given in milliseconds.
insertontime = 1000,
insertwidth = 20,
justify = "right", # right,left,center
# invalidcommand =
# invcmd =
readonlybackground = "orange", # The background color to use when the state is “readonly”.
relief = "groove", # flat,raised,ridge,sunken
selectbackground = "black",
selectborderwidth = 2,
selectforeground = "white",
show = "", # Controls how to display the contents of the widget.
state = "normal", # disabled,readonly
# takefocus =
validate = "none" # focusin,focusout,focus
# validatecommand =
# vcmd = # same as validatecommand
# xscrollcommand = # Used to connect an entry field to a horizontal scrollbar. This option should be set to the set method of the corresponding scrollbar.
)
value2 = value.get()
value.set("hello python ...")
entry.focus_set()
entry.delete(0,2)
entry.icursor(3) # Moves the insertion cursor to the given index.
index = entry,index(2) # Gets the numerical position corresponding to the given index.
entry.insert(0,"aaaaaa")
# scan_dragto(x), # Sets the scanning anchor for fast horizontal scrolling to the given mouse coordinate.
# scan_mark(x),# Scrolls the widget contents sideways according to the given mouse coordinate. The text is moved 10 times the distance between the scanning anchor and the new position.
#
# select_adjust(index) [#]
# Same as selection_adjust.
#
# select_clear() [#]
# Same as selection_clear.
#
# select_from(index) [#]
# Same as selection_from.
#
# select_present() [#]
# Same as selection_present.
#
# select_range(start, end) [#]
# Same as selection_range.
#
# select_to(index) [#]
# Same as selection_to.
#
# selection_adjust(index)
# Adjusts the selection to include also the given character. If index is already selected, do nothing.
#
# selection_clear()
# Clears the selection.
#
# selection_from(index)
# Starts a new selection. This also sets the ANCHOR index.
#
# selection_present()
# Checks if text is selected.
#
# lection_range(start, end)
# Explicitly sets the selection range. Start must be smaller than end. Use selection_range(0, END) to select all text in the widget.
#
# selection_to(index)
# Selects all text between ANCHOR and the given index.
#
# xview(index)
# Makes sure the given index is visible. The entry view is scrolled if necessary.
#
# xview_moveto(fraction)
# Adjusts the entry view so that the given offset is at the left edge of the canvas. Offset 0.0 is the beginning of the entry string, 1.0 the end.
#
# xview_scroll(number, what)
# Scrolls the entry view horizontally by the given amount.
entry.pack()

4. Text

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
# .config(**options)
# Text
text = tkinter.Text(
# maxundo =
# relief =
# setgrid = #Default value is 0.
# spacing1 = # Default value is 0.
# spacing2 = Default value is 0.
# spacing3 = # Default value is 0.
# state = # Default value is NORMAL.
# tabs = # No default value.
# takefocus= # No default value.
# undo = # Default is 0.
# wrap = #Default value is CHAR.
# xscrollcommand =
# yscrollcommand =
)
# text.tag_config("a", foreground="blue", underline=1)
# text.tag_bind("Enter>", show_hand_cursor)
# text.tag_bind("Leave>", show_arrow_cursor)
# text.tag_bind("Button-1>", click)
# text.config(cursor="arrow")
# text.insert("insert", "click here!", "a")
# text.tag_config("n", background="yellow", foreground="red")
# text.tag_config("a", foreground="blue")
# text.insert("contents", ("n", "a"))
# text.insert("end","aaa") # end,current,insert,sel_first,sel_last
# text.insert(1.0,'0123456789\n') # x.y
# text.insert("1.end",'zzz') # x.end
text.pack()

5. Button

1
2
3
4
5
6
7
8
9
10
11
12
def call():
print("call pressed ...")
button = tkinter.Button(
root,
command = call,
# default = # If set, the button is a default button. Tkinter will indicate this by drawing a platform specific indicator (usually an extra border). The default is DISABLED (no default behavior).
# overrelief = # Alternative relief to use when the mouse is moved over the widget. If empty, always use the relief value.
)
button.flash() # Flash the button. This method redraws the button several times, alternating between active and normal appearance.
button.invoke() # Call the command associated with the button.
button.pack()

6. Checkbutton

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# config(**options)
val = tkinter.StringVar()
def cbcall():
val.set(val.get()+"...")
print (val.get())
checkButton = tkinter.Checkbutton(
root,
text = "color",
variable = val,
onvalue = "blue",
offvalue ="none",
command = cbcall
)
# deselect() # Deselects the checkbox; that is, sets the value to offvalue.
# flash() # Redraws the button several times, alternating between active and normal appearance.
# invoke() # Calls the command associated with the button.
# select() # Selects the button; that is, sets the value to onvalue.
# toggle() # Toggles the button.
checkButton.pack()

7. Rudiobutton

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
# config(**options)
val = tkinter.StringVar()
def radcall():
print(val.get())
r1 = tkinter.Radiobutton(
root,
text = "one",
variable = val, # the value of the value
# textvariable = # the value of the text
value = "one1",
command = radcall
)
r1.pack()
r2 = tkinter.Radiobutton(
root,
text = "two",
variable = val,
value = "two2",
command = radcall
)
r2.pack()
# deselect() # Deselects the button.
# flash() # Redraws the button a couple of times, alternating between active and normal appearance. This can be useful when debugging, or to indicate when some other user action has activate the button.
# invoke() # Calls the command associated with the button.
# select() # Selects the button.

8. Listbox

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
# config(**options)
listbox = tkinter.Listbox(
root,
selectmode = "extended", # browse
activestyle = "underline",
# exportselection =
# listvariable =
# xscrollcommand =
# yscrollcommand =
)
listbox.pack()
for x in [x*2 for x in range(10)]:
listbox.insert("end",x)
# lb.bind("<Double-Button-1>", self.ok)
# activate(index)
# bbox(self, index)
# curselection()
# delete(first, last=None)
# get(first, last=None)
# index(index)
# insert(index, *elements)
# itemconfig(index, **options)
# selection_anchor(index) # Sets the selection anchor to the given index. The anchor can be refered to using the ANCHOR index.
# selection_clear(first, last=None) # Removes one or more items from the selection.
# selection_includes(index) # Checks if an item is selected.
# selection_set(first, last=None) # Adds one or more items to the selection.
# size()
# xview(column, *extra) # Controls horizontal scrolling.
# yview(*what)
# yview_scroll(number, what)
# xview_scroll(number, what)
# yview_moveto(fraction)
# xview_moveto(fraction)

×

纯属好玩

扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

文章目录
  1. 1. 1. Label
  2. 2. 2. Frame
  3. 3. 3. Entry
  4. 4. 4. Text
  5. 5. 5. Button
  6. 6. 6. Checkbutton
  7. 7. 7. Rudiobutton
  8. 8. 8. Listbox
,