I have written this code and in this the function gen() id used to generate numbers at random for sorting. My code is like
from Tkinter import *
import random
class Sorting( Frame ):
def __init__( self ):
Frame.__init__( self )
self.master.title( "Sorting" )
self.master.rowconfigure( 5, weight = 1 )
self.master.columnconfigure( 5, weight = 1 )
self.grid( sticky = W+E+N+S )
#label for sort intro
self.label1 = Label( self, text = "Select Sort", width = 25 , height=2)
self.label1.grid( row = 0, column = 1, sticky = N )
#Radio buttons for sorts
self.button1 = Radiobutton( self, text = "Bubble Sort" )
self.button1.grid( row = 1, column = 0, sticky = W+E+N+S )
self.button2 = Radiobutton( self, text = "Quick Sort" )
self.button2.grid( row = 1, column = 1, sticky = W+E+N+S )
self.button3 = Radiobutton( self, text = "Shell Sort" )
self.button3.grid( row = 1, column = 2, sticky = W+E+N+S )
#label to store value
def gen():
for x in range(0,10):
num=random.randint(0,100)
self.label2 = Label( self,text='%s'%num, width = 2, height = 2 )
self.label2.grid( row =3 , columnspan =10 , sticky = W+E+N+S )
#button to generate number
self.button4 = Button( self,text='Generate no.', command=gen )
self.button4.grid( row = 2,column=1, sticky = W+E+N+S )
self.rowconfigure( 5, weight = 1 )
self.columnconfigure( 5, weight = 1 )
def main():
Sorting().mainloop()
if __name__ == "__main__":
main()
I want to use it for generating random numbers and then sorting them. Any suggestion to do it.
Try this:
The values to sort are stored in
self.nums. You want to call your sorting algorithm to that list before showing them in the label.I tried to keep your code as it was as much as possible. From this point it can be further optimized. For example, you could substitute:
with