Tkinter在python中选择不同排序的GUI
我将此代码用于单选按钮:Tkinter在python中选择不同排序的GUI,python,user-interface,tkinter,Python,User Interface,Tkinter,我将此代码用于单选按钮: v= IntVar() self.button1 = Radiobutton( self, text = "Bubble Sort" ,variable=v,value=1) self.button1.grid( row = 1, column = 0, sticky = W+E+N+S ) self.button2 = Radiobutton( self, text = "Quick Sort",variable=v,value=2) sel
v= IntVar()
self.button1 = Radiobutton( self, text = "Bubble Sort" ,variable=v,value=1)
self.button1.grid( row = 1, column = 0, sticky = W+E+N+S )
self.button2 = Radiobutton( self, text = "Quick Sort",variable=v,value=2)
self.button2.grid( row = 1, column = 1, sticky = W+E+N+S )
self.button3 = Radiobutton( self, text = "Shell Sort", variable=v,value=3)
self.button3.grid( row = 1, column = 2, sticky = W+E+N+S )
以上是完整代码,屏幕截图如下:
如何在选择排序后单击按钮对这些数字进行排序?我是否必须首先从三个给定值中选择排序,然后单击“排序”按钮对数字进行排序
以下是剩下的:
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
v= IntVar()
self.button1 = Radiobutton( self, text = "Bubble Sort" ,variable=v,value=1)
self.button1.grid( row = 1, column = 0, sticky = W+E+N+S )
self.button2 = Radiobutton( self, text = "Quick Sort",variable=v,value=2)
self.button2.grid( row = 1, column = 1, sticky = W+E+N+S )
self.button3 = Radiobutton( self, text = "Shell Sort", variable=v,value=3)
self.button3.grid( row = 1, column = 2, sticky = W+E+N+S )
#function to do soting
#def sort():
#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)
#self.label2.pack(fill="both")
#Button for sorting
self.button5=Button(self,text='start sorting')
self.button5.grid( row = 4,column=1, 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( 1, weight = 1 )
self.columnconfigure( 1, weight = 1 )
def main():
Sorting().mainloop()
if __name__ == "__main__":
main()
您必须为“开始排序”按钮实现回调方法: 在self.v.get中按下按钮。 此整数值用作存储排序方法名称的字典的键:
self.function = {0:self.bubble, 1:self.quick, 2:self.shell}
然后result=函数给出调用相应排序方法的结果。您仍然需要定义这些方法:
def bubble(self):
print('bubble to be implemented')
return sorted(self.nums)
def shell(self):
print('shell to be implemented')
return sorted(self.nums)
def quick(self):
print('quick to be implemented')
return sorted(self.nums)
这是完整的代码:
import random
from tkinter import *
class Sorting(Frame):
def __init__(self):
Frame.__init__(self)
self.function = {0:self.bubble, 1:self.quick, 2:self.shell}
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.v = IntVar()
for indx, button in enumerate(('Bubble', 'Quick', 'Shell')):
name = "%s Sort" % button
button = Radiobutton(self, text=name, variable=self.v, value=indx)
button.grid(row=1, column=indx, sticky=W+E+N+S)
button.deselect()
#button to generate number
self.button4 = Button(self,text='Generate no.',command=self.gen)
self.button4.grid(row=2, column=1, sticky=W+E+N+S)
self.rowconfigure(5, weight=1)
self.columnconfigure(5, weight=1)
def create_but2sort(self):
self.button5 = Button(self, text='start sorting', command=self.sortit)
self.button5.grid(row=4, column=1, sticky=W+E+N+S)
self.rowconfigure(5, weight=1 )
self.columnconfigure(5, weight=1)
def gen(self):
self.nums = [random.randint(0, 100) for x in range(10)]
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)
self.create_but2sort()
def sortit(self):
function = self.function[self.v.get()]
result = function()
num = ''.join('%4i' % num for num in result)
self.label3 = Label(self, text=num, width=2, height=2)
self.label3.grid(row=5, columnspan=10, sticky=W+E+N+S )
def bubble(self):
print('bubble to be implemented')
return sorted(self.nums)
def shell(self):
print('shell to be implemented')
return sorted(self.nums)
def quick(self):
print('quick to be implemented')
return sorted(self.nums)
def main():
Sorting().mainloop()
if __name__ == "__main__":
main()
不,您发布的完整代码没有生成您的图像。如果定义了“开始排序”按钮,为什么按钮1-3的定义与您文章的第一个代码段中的不同?对不起,我错误地发布了旧的代码对不起,给您带来不便。我已经更正了代码是不是这段代码对所有三种排序都使用了相同的技术…没有不同排序的算法…那么是什么用于对它们进行排序。'shell to implemented'、'bubble to implemented'、'quick to implemented'。。。谢谢你,我想…是的,我知道我必须实现那些我只是不明白,但现在我知道…你可以检查一下问题了吗
import random
from tkinter import *
class Sorting(Frame):
def __init__(self):
Frame.__init__(self)
self.function = {0:self.bubble, 1:self.quick, 2:self.shell}
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.v = IntVar()
for indx, button in enumerate(('Bubble', 'Quick', 'Shell')):
name = "%s Sort" % button
button = Radiobutton(self, text=name, variable=self.v, value=indx)
button.grid(row=1, column=indx, sticky=W+E+N+S)
button.deselect()
#button to generate number
self.button4 = Button(self,text='Generate no.',command=self.gen)
self.button4.grid(row=2, column=1, sticky=W+E+N+S)
self.rowconfigure(5, weight=1)
self.columnconfigure(5, weight=1)
def create_but2sort(self):
self.button5 = Button(self, text='start sorting', command=self.sortit)
self.button5.grid(row=4, column=1, sticky=W+E+N+S)
self.rowconfigure(5, weight=1 )
self.columnconfigure(5, weight=1)
def gen(self):
self.nums = [random.randint(0, 100) for x in range(10)]
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)
self.create_but2sort()
def sortit(self):
function = self.function[self.v.get()]
result = function()
num = ''.join('%4i' % num for num in result)
self.label3 = Label(self, text=num, width=2, height=2)
self.label3.grid(row=5, columnspan=10, sticky=W+E+N+S )
def bubble(self):
print('bubble to be implemented')
return sorted(self.nums)
def shell(self):
print('shell to be implemented')
return sorted(self.nums)
def quick(self):
print('quick to be implemented')
return sorted(self.nums)
def main():
Sorting().mainloop()
if __name__ == "__main__":
main()