在Python tkinter中实现了开始,如何跳出循环 求大神给看看

X530654040 2019-04-17 03:53:48
import tkinter.filedialog
from tkinter import *
import random
import time

yaohao_window = tkinter.Tk()
yaohao_window.geometry('600x600')
yaohao_window.title('摇号系统')

text = StringVar()
text.set("0")
a = StringVar()
a.set("1")
def k( ):
text.set(str(random.randint(1, 50)))
time.sleep(1)
yaohao_window.update()
k( )
def g( ):
a="2"





b = Button(yaohao_window, text='开始', font=('KaiTi', 36, 'bold'), bd=2, width=4, command=k)
b.pack()
b2 = Button(yaohao_window, text='结束', font=('KaiTi', 36, 'bold'), bd=2, width=4, command=g)
b2.pack()
l = Label(yaohao_window, bg='yellow', font=('KaiTi', 10, 'bold'), width=40, textvariable=text, )
l.pack(side=BOTTOM, expand=YES, fill=BOTH)





yaohao_window.mainloop()
...全文
500 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
陈年椰子 2019-04-18
  • 打赏
  • 举报
回复
我想了一下, 可能上面的代码并不工作, 如果不工作, 你考虑一下线程吧。
陈年椰子 2019-04-18
  • 打赏
  • 举报
回复
tk 没怎么用过, 不过你这个 k() 递归调用,堆栈到一定程度就挂了。 改成这样试试。


loop_flag = 1

def k( ):
    global loop_flag
    while loop_flag:
        text.set(str(random.randint(1, 50)))
        time.sleep(1)
        yaohao_window.update()

def g( ):
    a="2"
    loop_flag = 0


X530654040 2019-04-17
  • 打赏
  • 举报
回复
如何实现点击开始循环出数,点击结束暂停出数呢?

37,720

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧