0

I want to generate a random array in a function. After that I want to sort this array in another function. How I can I access the array in second function?

from tkinter import *
import array as arr
import numpy as np
import random
root = Tk()

        Array_Size=IntVar()
        Minim_Elem=IntVar()
        Maxim_Elem=IntVar()
        
        def Genera_Array():
            
            ArraySize=Array_Size.get()
            MinimumElement=Minim_Elem.get()
            MaximumElement=Maxim_Elem.get()
            Arr=[ArraySize]
        
            for H in range(0, ArraySize):
                Arr.append(random.randint(MinimumElement,MaximumElement))
                
            for A in range(0,ArraySize):
                Output_1.insert(END,Arr[A])
                Output_1.insert(END, " ")     
            
            return Arr,ArraySize
        
        def QuickSort():
        
            print("Array before sorting")
            for A in range(0,ArraySize):
                print(Arr[ArraySize], end=" ")
    
    
    Gene_Array=Button(text="Generate Array", command=Genera_Array)
    Gene_Array.pack()
    Gene_Array.place(x=295,y=245, width=240,height=40)
    
    Quick=Button(text="Quick Sort", command=QuickSort)
    Quick.pack()
    Quick.place(x=410,y=410, width=120,height=40)

Output_1=Text(root, width=56, height=3, bd=5, font=("Time new roman",12,"bold"))
Output_1.place(x=150,y=300)
root.mainloop()
quamrana
  • 37,849
  • 12
  • 53
  • 71

2 Answers2

0

you need to pass a data to

def Quicksort(ArrazSize):

using lambda to pass parameter in function

in Gene_Array=Button(text="Generate Array", command=lambda: QuickSort(Arraysize))

after getting data from

Gene_Array
0

Here is a sample program. However it does still have some flaws. For example the quicksort does not seem to work. But apparently the Arr object can be accessed within the QuickSort function.

from tkinter import *
import array as arr
import numpy as np
import random

class Program():
    def __init__(self,size,min_,max_):
        root = Tk()

        self.Array_Size=IntVar(value=size)
        self.Minim_Elem=IntVar(value=min_)
        self.Maxim_Elem=IntVar(value=max_)
        Gene_Array=Button(root,text="Generate Array", command=self.Genera_Array)
        Gene_Array.pack()
        #Gene_Array.place(x=295,y=245, width=240,height=40)
        
        Quick=Button(root,text="Quick Sort", command=self.QuickSort)
        Quick.pack()
        #Quick.place(x=410,y=410, width=120,height=40)
        self.Output_1=Text(root, width=56, height=3, bd=5, font=("Time new roman",12,"bold"))
        self.Output_1.pack()
        root.mainloop()
        
    def Genera_Array(self):
        
        self.ArraySize=self.Array_Size.get()
        MinimumElement=self.Minim_Elem.get()
        MaximumElement=self.Maxim_Elem.get()
        self.Arr=[self.ArraySize]
    
        for H in range(0, self.ArraySize):
           self.Arr.append(random.randint(MinimumElement,MaximumElement))
            
        for A in range(0,self.ArraySize):
            self.Output_1.insert(END,self.Arr[A])
            self.Output_1.insert(END, " ")     
        
        
    def QuickSort(self):
    
        print("Array before sorting")
        for A in range(0,self.ArraySize):
            print(self.Arr[self.ArraySize], end=" ")
    

p=Program(10,3,8) 
Dharman
  • 30,962
  • 25
  • 85
  • 135
angelogro
  • 124
  • 8