Python Forum
How to break out of a for loop on button press?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to break out of a for loop on button press?
#1
from tkinter import *

# root
root = Tk()
root.title('SeqTank')
scrwidth = root.winfo_screenwidth()
scrheight = root.winfo_screenheight()
root.geometry("%sx%s+%s+%s" % (int(scrwidth / 1.5), 
                                int(scrheight / 1.25), 
                                int(scrwidth / 6), 
                                int(scrheight / 12)))
root.update()

# paned master with two panels
panedmaster = PanedWindow(root, orient='v', sashwidth=1, relief='flat', sashcursor='arrow', bg='#666666')
panedmaster.place(relwidth=1, relheight=1)
uppanel = PanedWindow(panedmaster, height=35, relief='flat')
panedmaster.add(uppanel)
downpanel = PanedWindow(panedmaster, relief='flat')
panedmaster.add(downpanel)

text_editor = Text(downpanel, 
                    bg='black', 
                    fg='#32a852',
                    insertbackground='white',
                    font=('Courier', 24))
text_editor.place(relwidth=1, 
                    relheight=1)
text_editor.focus_set()


generate_button = Button(uppanel, text='Play!')
generate_button.place(relwidth=1)

def process():
	for i in range(100):
		...
		# playing a midi-file here


generate_button.configure(command=process)

root.mainloop()
This is my simple tkinter interface. I created a little tool that generates a midi-file based on the text input(it uses a special lilypond-like syntax) and plays the midi file using mido in a for loop. My question is how can I break the for loop if I press the play button again before it finished the for loop? I hope someone can tell me in (regarding the above example) how I should write it different...
Reply


Messages In This Thread
How to break out of a for loop on button press? - by philipbergwerf - Oct-04-2022, 03:59 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Button to stop while loop from another script Absolutewind 5 973 Sep-25-2023, 11:20 PM
Last Post: deanhystad
  Code won't break While loop or go back to the input? MrKnd94 2 1,002 Oct-26-2022, 10:10 AM
Last Post: Larz60+
  break out of for loop? User3000 3 1,492 May-17-2022, 10:18 AM
Last Post: User3000
  Asyncio: Queue consumer gets out of while loop without break. Where exactly and how? saavedra29 2 2,726 Feb-07-2022, 07:24 PM
Last Post: saavedra29
  tkinter auto press button kucingkembar 2 3,232 Dec-24-2021, 01:23 PM
Last Post: kucingkembar
  tkinter control break a while loop samtal 0 2,421 Apr-29-2021, 08:26 AM
Last Post: samtal
  Cannot 'break' from a "for" loop in a right place tester_V 9 4,045 Feb-17-2021, 01:03 AM
Last Post: tester_V
  How to break a loop in this case? Blainexi 10 7,352 Sep-24-2020, 04:06 PM
Last Post: Blainexi
  how to break the loop? bntayfur 8 3,106 Jun-07-2020, 11:07 PM
Last Post: bntayfur
  break for loop Agusben 1 1,950 Apr-01-2020, 05:07 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020