0

我想在函数中生成一个随机数组。之后,我想在另一个函数中对该数组进行排序。如何在第二个函数中访问数组?

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()
4

2 回答 2

0

您需要将数据传递给

def Quicksort(ArrazSize):

用于lambda在函数中传递参数

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

从获取数据后

Gene_Array
于 2020-12-11T10:53:22.063 回答
0

这是一个示例程序。但是,它仍然存在一些缺陷。例如,快速排序似乎不起作用。但显然Arr可以在QuickSort函数内访问该对象。

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) 
于 2020-12-11T11:07:29.357 回答