Форум сайта python.su
-8
Я хочу сделать с помощью tkinter так, чтобы при нажатии на кнопку в Label добавлялось одно ‘X’ кажд1ю секунду.
Вот как сделал:
from tkinter import * import time root = Tk() def cicle(): global texter, oper for c in range(0, 10): texter = texter + 'X' oper["text"] = texter oper.pack(side='top', fill='x') time.sleep(1) texter = 'X' oper = Label(root, text=texter, width=3, height=1) oper.pack(side='top', fill='x') start = Button(root, text='Start', command=cicle) start.pack(side='bottom', fill='x') root.mainloop()
Отредактировано python335 (Дек. 11, 2016 18:17:05)
Офлайн
294
честно говоря в ТК не силен, но как я понимаю тут дело не в цикле а в принципе работы Button-а(или mainloop ткинтера), вы не увидите результата нажатия пока процедура не отработает полностью и не вернет какойнибудь “финишед”… первое что приходит в голову это использовать threading
from tkinter import * import time import threading root = Tk() def run(): t1 = threading.Thread(target=cicle) t1.start() def cicle(): global texter, oper for c in range(0, 10): texter = texter + 'X' oper["text"] = texter #oper.pack(side='top', fill='x') time.sleep(1) texter = 'X' oper = Label(root, text=texter, width=3, height=1) oper.pack(side='top', fill='x') start = Button(root, text='Start', command=run) start.pack(side='bottom', fill='x') root.mainloop()
[code python][/code]
Отредактировано PEHDOM (Дек. 12, 2016 09:42:06)
Офлайн
294
я лошара, все гораздо проще, ну и + я таки совсем плохо знаю ткинтер. код без многопоточности:
from tkinter import * import time root = Tk() def cicle(): global texter, oper for c in range(0, 10): texter = texter + 'X' oper["text"] = texter #oper.pack(side='top', fill='x') oper.update() time.sleep(1) texter = 'X' oper = Label(root, text=texter, width=3, height=1) oper.pack(side='top', fill='x') start = Button(root, text='Start', command=cicle) start.pack(side='bottom', fill='x') root.mainloop()
[code python][/code]
Отредактировано PEHDOM (Дек. 13, 2016 13:07:26)
Офлайн