Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我想用Tkinter在python中创建用于排序的gui应用程序_Python_User Interface_Tkinter - Fatal编程技术网

我想用Tkinter在python中创建用于排序的gui应用程序

我想用Tkinter在python中创建用于排序的gui应用程序,python,user-interface,tkinter,Python,User Interface,Tkinter,我编写了这段代码,其中的函数gen()id用于随机生成数字进行排序。我的代码是 from Tkinter import * import random class Sorting( Frame ): def __init__( self ): Frame.__init__( self ) self.master.title( "Sorting" ) self.master.rowconfigure( 5, weight = 1 ) sel

我编写了这段代码,其中的函数gen()id用于随机生成数字进行排序。我的代码是

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()
我想用它来生成随机数,然后对它们进行排序。有什么建议吗。

试试这个:

  #label to store value
  def gen():
      self.nums = []
      for x in range(0, 10):
          self.nums.append(random.randint(0, 100))
      # . . . . . . . . . .  .    <- maybe here call sorting method on self.nums
      num = ''.join('%4i' % num for num in self.nums)
      self.label2 = Label( self, text=num, width=2, height=2)
      self.label2.grid(row=3, columnspan=10, sticky=W+E+N+S)

试试这个:

  #label to store value
  def gen():
      self.nums = []
      for x in range(0, 10):
          self.nums.append(random.randint(0, 100))
      # . . . . . . . . . .  .    <- maybe here call sorting method on self.nums
      num = ''.join('%4i' % num for num in self.nums)
      self.label2 = Label( self, text=num, width=2, height=2)
      self.label2.grid(row=3, columnspan=10, sticky=W+E+N+S)


1) 你应该更好地解释你想要得到什么。画个草图会有帮助的。2) 您想编辑代码并修复缩进错误。我编辑了它,您现在可以检查它是否仍然缩进良好。如果您使用标准的4个空格缩进而不是2个空格缩进,则问题所在的位置对您来说更为明显。这是遵循PEP-8风格指南建议的优势的实际证明。1)你应该更好地解释你想要获得什么。画个草图会有帮助的。2) 您想编辑代码并修复缩进错误。我编辑了它,您现在可以检查它是否仍然缩进良好。如果您使用标准的4个空格缩进而不是2个空格缩进,则问题所在的位置对您来说更为明显。这是遵循PEP-8风格指南建议的优点的实际证明。@user1155111完美!请参见我的上一次编辑。您应该接受最佳答案(单击投票箭头下方的标记)。你还没有接受你对其他问题的任何回答。我在下面问了一些问题,我如何使用一个函数的数组another@user1155111完美的请参见我的上一次编辑。您应该接受最佳答案(单击投票箭头下方的标记)。你还没有接受其他问题的答案。我在下面问了一些问题,我如何在另一个函数中使用一个函数的数组
  self.nums = [random.randint(0, 100) for x in range(10)]