我将此代码用于单选按钮:
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()
答案 0 :(得分:2)
您必须为start sorting
按钮实施回调方法:
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 )
在self.v.get()
按下按钮。
此整数值用作存储排序方法名称的字典的键:
self.function = {0:self.bubble, 1:self.quick, 2:self.shell}
然后result = function()
为您提供调用相应排序方法的结果。您还要定义这些方法:
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()