具体参考哪位大佬的,记不太清楚了。
直接上代码,大体逻辑是这样的。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
# -*- coding:utf-8 -*- from tkinter import * root = tk() def g(): if b[ 'text' ] = = '开始' : b[ 'text' ] = '关闭' print ( '开始' ) else : b[ 'text' ] = '开始' print ( '结束' ) b = button(root,text = '开始' ,font = ( 'kaiti' , 36 , 'bold' ),bg = 'pink' ,fg = 'green' ,bd = 2 ,width = 10 ,command = g) b.pack() root.mainloop() |
说一说我遇到的问题:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
from tkinter import * from tkinter import ttk from tkinter.messagebox import * import os #log日志 class countframe(frame): # 继承frame类 def __init__( self , master = none): frame.__init__( self , master) self .root = master # 定义内部变量root self .createpage() def createpage( self ): self .b = button( self , text = '开始' , width = 12 , command = self .switch).grid(row = 6 , stick = e, pady = 10 ) def switch( self ): if self .b[ 'text' ] = = '开始' : self .b[ 'text' ] = '结束' print ( 1 ) else : self .b[ 'text' ] = '开始' |
我想让这个开始按钮,点击变成开始,再次点击变成结束。
使用上面的方法是失败的,我发现self.b打印出来为空。
1
2
|
self .b = button( self , text = '开始' , width = 12 , command = self .switch).grid(row = 6 , stick = e, pady = 10 ) grid(row = 6 , stick = e, pady = 10 ) |
是因为后面调用了grid方法导致的,两个对象公用一个对象有冲突,(grid为展示方法),一个调用,一个展示,把他们两个分开写。
下面是最终的代码:
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
from tkinter import * from tkinter import ttk from tkinter.messagebox import * import os #基础功能 class inputframe(frame): # 继承frame类 def __init__( self , master = none): frame.__init__( self , master) self .root = master # 定义内部变量root self .itemname = stringvar() self .importprice = stringvar() self .sellprice = stringvar() self .deductprice = stringvar() self .createpage() def createpage( self ): label( self ).grid(row = 0 , stick = w, pady = 10 ) button( self ,text = '截屏' ,width = 25 ,height = 5 ,).grid(row = 1 , stick = w, pady = 10 ) button( self , text = '录像' ,width = 25 ,height = 5 ,).grid(row = 2 , stick = w, pady = 10 ) #mongkey class queryframe(frame): # 继承frame类 def __init__( self , master = none): frame.__init__( self , master) self .root = master # 定义内部变量root self .itemname = stringvar() self .importprice = stringvar() self .sellprice = stringvar() self .deductprice = stringvar() self .number = stringvar() self .createpage() def createpage( self ): label( self ).grid(row = 0 , stick = w, pady = 10 ) label( self , text = '包名: ' ).grid(row = 1 , stick = w, pady = 10 ) entry( self , textvariable = self .itemname).grid(row = 1 , column = 1 , stick = e) label( self , text = '压力: ' ).grid(row = 2 , stick = w, pady = 10 ) entry( self , textvariable = self .importprice).grid(row = 2 , column = 1 , stick = e) label( self , text = '标记: ' ).grid(row = 3 , stick = w, pady = 10 ) entry( self , textvariable = self .sellprice).grid(row = 3 , column = 1 , stick = e) # 创建一个下拉列表 label( self , text = '日志级别: ' ).grid(row = 4 , stick = w, pady = 10 ) numberchosen = ttk.combobox( self , width = 12 , textvariable = self .number, state = 'readonly' ) numberchosen[ 'values' ] = ( 1 , 2 , 3 ) # 设置下拉列表的值 numberchosen.grid(column = 1 , row = 4 ,stick = e) # 设置其在界面中出现的位置 column代表列 row 代表行 numberchosen.current( 1 ) # 设置下拉列表默认显示的值,1为 numberchosen['values'] 的下标值 checkbutton( self ,text = '忽略崩溃' ).grid(row = 5 , column = 1 , stick = e) checkbutton( self , text = '忽略超时' ).grid(row = 6 , column = 1 , stick = e) checkbutton( self , text = '崩溃日志' ).grid(row = 7 , column = 1 , stick = e) button( self , text = '开始压测' ).grid(row = 8 , column = 5 , stick = e, pady = 5 ) button( self , text = '报告分析' ).grid(row = 9 , column = 5 , stick = e, pady = 5 ) #log日志 class countframe(frame): # 继承frame类 def __init__( self , master = none): frame.__init__( self , master) self .root = master # 定义内部变量root self .loglevel = stringvar() #日志级别 self .label = stringvar() #过滤标签 self .filterformat = stringvar() #过滤项格式 self .createpage() def createpage( self ): label( self , text = '过滤标签: ' ).grid(row = 1 , stick = w, pady = 10 ) entry( self , textvariable = self .label).grid(row = 1 , column = 1 , stick = e) label( self , text = '过滤字符串: ' ).grid(row = 2 , stick = w, pady = 10 ) entry( self , textvariable = self .label).grid(row = 2 , column = 1 , stick = e) label( self , text = '正则过滤: ' ).grid(row = 3 , stick = w, pady = 10 ) entry( self , textvariable = self .label).grid(row = 3 , column = 1 , stick = e) #日志级别############################################################ # 创建一个下拉列表 label( self , text = '日志级别: ' ).grid(row = 4 , stick = w, pady = 10 ) label( self ).grid(row = 0 , stick = w, pady = 10 ) numberchosen = ttk.combobox( self , width = 12 , textvariable = self .loglevel, state = 'readonly' ) numberchosen[ 'values' ] = ( 1 , 2 , 3 ) # 设置下拉列表的值 numberchosen.grid(column = 1 , row = 4 , stick = e) # 设置其在界面中出现的位置 column代表列 row 代表行 numberchosen.current( 1 ) # 设置下拉列表默认显示的值,1为 numberchosen['values'] 的下标值 #过滤格式############################################################ label( self , text = '过滤项格式: ' ).grid(row = 5 , stick = w, pady = 10 ) label( self ).grid(row = 0 , stick = w, pady = 10 ) filterformat = ttk.combobox( self , width = 12 , textvariable = self .filterformat, state = 'readonly' ) #-- v : verbose (明细); #-- d : debug (调试); #-- i : info (信息); #-- w : warn (警告); #-- e : error (错误); #-- f : fatal (严重错误); filterformat[ 'values' ] = ( 'verbose' , 'debug' , 'warn' , 'error' , 'fatal' ) # 设置下拉列表的值 filterformat.grid(column = 1 , row = 5 , stick = e) # 设置其在界面中出现的位置 column代表列 row 代表行 filterformat.current( 1 ) # 设置下拉列表默认显示的值,1为 numberchosen['values'] 的下标值 self .b = button( self , text = '开始' , width = 12 ,command = self .switch ) self .b.grid(row = 6 , stick = e, pady = 10 ) # button(self, text='生成日志', width=12, ).grid(row=6, stick=e, pady=10) def switch( self ): if self .b[ 'text' ] = = '开始' : self .b[ 'text' ] = '结束' print ( 1 ) else : self .b[ 'text' ] = '开始' #性能测试界面 class aboutframe(frame): # 继承frame类 def __init__( self , master = none): frame.__init__( self , master) self .root = master # 定义内部变量root self .createpage() def createpage( self ): label( self , text = '性能测试开发中' ).pack() #设置页面 class seetingframe(frame): # 继承frame类 def __init__( self , master = none): frame.__init__( self , master) self .root = master # 定义内部变量root self .createpage() def createpage( self ): label( self , text = '设置开发中' ).pack() |
以上这篇python-tkinter之按钮的使用,开关方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/qq_39208536/article/details/80902698