繁体   English   中英

如何在 python ZE5BA8B4C39C2087BF134Z26EF5F14Z 中使用一个 function 的数组到另一个 function 中?

[英]How can I use the array of one function into another function in python tkinter?

我想在 function 中生成一个随机数组。 之后,我想在另一个 function 中对该数组进行排序。 如何访问第二个 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()

您需要将数据传递给

def Quicksort(ArrazSize):

使用lambda在 function 中传递参数

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

从获取数据后

Gene_Array

这是一个示例程序。 但是,它仍然存在一些缺陷。 例如,快速排序似乎不起作用。 但显然Arr object 可以在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) 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM