【问题标题】:How can I use the array of one function into another function in python tkinter?如何在 python tkinter 中将一个函数的数组用于另一个函数?
【发布时间】:2020-12-11 10:43:15
【问题描述】:

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

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

【问题讨论】:

    标签: python function tkinter-button


    【解决方案1】:

    您需要将数据传递给

    def Quicksort(ArrazSize):
    

    使用lambda在函数中传递参数

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

    获取数据后

    Gene_Array
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 非常感谢您帮助我。
      猜你喜欢
      • 1970-01-01
      • 2021-03-22
      • 1970-01-01
      • 1970-01-01
      • 2014-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-26
      相关资源
      最近更新 更多