问题遇到的现象和发生背景
我在写一个带界面的python(GUI)编程的过程中遇到了一个问题,例如:有两个按钮一个是“开始”一个是“停止”。点了开始之后会运行绑定好的函数,为了让这个函数一直循环的跑,我加了个while循环。在点击开始程序已经运行的过程中,我想如果当我要停止下来时,点击“停止”按钮就会终止当前运行的函数。所以我一开始的思路是,在while循环里面加了个条件(while state == True),当我点击停止时使得state = False。但是不行,我点停止的时候它会报一个“未响应”给我。有没有朋友知道该怎么写
以下是部分相关的代码
class Application(Frame):
def createWidegt(self):
self.btn04 = Button(self, text='开始投注', command=self.start_betting_for, state='disabled')
self.btn04.pack(side='left')
self.btn06 = Button(self, text='立即停止', command=self.stop_now)
self.btn06.pack(side='left')
def stop_now(self):
global state
state = False
def start_betting_for(self):
global cookies
global account
global number
global state
while state == True:
for i, j, k in zip(cookies, account, number):
txt = self.start_betting(i, k)
if txt == '请求成功':
print(f'账户{j}:投注成功')
else:
print(f'账户{j}:{txt}')
time.sleep(90)
print('--' * 20)
def start_betting(self, cookie, number):
# 投注请求头
headers_betting = {
'Host': 'd015.appapi01.com',
'accept-language': 'zh-CN,zh;q=0.8',
'user-agent': 'yicai/1.0.10 (Android 9; Xiaomi_MI 8) [com.tfx.yicai_1.0.10_1010]',
'content-type': 'application/x-www-form-urlencoded; charset=utf-8',
'content-length': '170',
'accept-encoding': 'gzip',
'cookie': cookie,
}
data_betting = {
'oooForce': 'false',
'text': f'[{{"code": "1972", "compress": "false", "content": "{number},-,-,-,-", "issue":"", "lottery": "hash60ssc", "method": "dw", "methodType": 1, "model": "li", "multiple": 1}}]'
}
# 投注接口
url_betting = 'https://d015.appapi01.com/api/game-lottery/add-order'
# 发起投注请求
response_betting = requests.post(url=url_betting, headers=headers_betting, data=data_betting)
value = json.loads(response_betting.text) # 将响应数据格式转换
return value['message']
相关截图
我想要达到的结果
看看有没有什么办法,先提前谢谢你们了