Write gui in python

At the end of the data structure course, I wrote a gui interface in python and used 6 sorting algorithms to sort the array. It feels quite interesting. It is the uncomfortable tossing about tkinter. There are too few documents. You can only use help to see some built-in methods. use##

Interface part code
First look at the ugly interface QAQ

# - *- coding: utf-8-*-from tkinter import*import datetime
import time
from data_sort import My_Sort
import random
import sys
import re
# Insert hill bubbling to quickly select heap sort
classApplication(Frame):
 def __init__(self,root):"""initialization,Create various buttons and text boxes"""
  # Create a number list that stores numbers
  self.number =[]
  # count is used to record the number of numbers to be sorted
  self.count =10086
  self.my =My_Sort()super(Application,self).__init__(root)
  self.grid()
  self.root = root
  Label(self, text="\t\t").grid(row=0, column=0)Label(self, text="Please select sort mode:",
    font ='STXingkai -25 bold', fg ="#30E6FF").grid(row=0, column=1)
  # Create a radio button to let the user select the data source
  self.choose =StringVar()
  self.choose.set(None)
  self.input_number =Radiobutton(self, text="1 Manually enter numbers", variable=self.choose,
          value="Please enter random numbers in the right dialog box, separated by spaces",
          command=self.update_text,
          justify="center",
          font='STXingkai -20 bold', fg="#DF4FE7").grid(row=2, column=0, sticky=W)
  self.rand_number =Radiobutton(self, text="2 Generate random numbers", variable=self.choose,
          value ="Please click the Generate Random Number button below",
          command = self.update_text,
          justify ="center",
          font ='STXingkai -20 bold', fg="#DF4FE7").grid(row=3, column=0, sticky=W)
  # Create buttons for each function
  self.srand_btn =Button(self, text ="Generate random numbers",
         command = self.create_rand_number
         ). grid(row=4, column=0, sticky=E,
          ipadx="4p", ipady="4p")
  self.get_txt_btn =Button(self, text="Read text box number",
        command=self.get_Text_number
        ). grid(row=4, column=1,
          ipadx="4p", ipady="4p")
  self.exit_btn =Button(self, text="exit the program", command=self.GuiExit,
        foreground="red", font='STXingkai -15 bold',).grid(row=4, column=2, ipadx="4p", ipady="4p")
  self.insert_btn =Button(self, text="Insertion sort",
         command =self.insert
         ). grid(row=5, column=0, ipadx="4p", ipady="4p")
  self.shell_btn =Button(self, text="Hill sort",
          command = self.shell
          ). grid(row=5, column=1, ipadx="4p", ipady="4p")
  self.buddle_btn =Button(self, text="Bubble sort",
         command = self.buddle
         ). grid(row=5, column=2, ipadx="4p", ipady="4p")
  self.quick_btn =Button(self, text="Quick sort",
        command = self.quick
        ). grid(row=6, column=0, ipadx="4p", ipady="4p")
  self.select_btn =Button(self, text="Select sort",
         command = self.select
         ). grid(row=6, column=1, ipadx="4p", ipady="4p")
  self.heap_btn =Button(self, text="Heap sort",
        command = self.heap
        ). grid(row=6, column=2, ipadx="4p", ipady="4p")
  self.view_gui =Button(self, text="View GUI source code",command = self.view_gui
        ). grid(row=7, column=0, ipadx="4p", ipady="4p")
  self.view_sort =Button(self, text="View sorting source code",command = self.view_sort
        ). grid(row =7, column=1, ipadx="4p", ipady="4p")Label(self, text="\t").grid(row=1, column=2)Label(self, text="Input and output box:", font='STXingkai -16 ',  fg="#F9A728").grid(row=1, column=3)Label(self,text="").grid(row=8,column=0)
  self.txt =Label(self, text="", width=37, height=5,borderwidth=2,
       foreground="black",background="#E0E0E0",
       font='STXingkai -20 ', fg="#4169E1")
  self.txt.grid(row=2, column=1, sticky=W)
  self.efficiency =Label(self, text="",
        width=35,height=10,borderwidth=2,
        font='STXingkai -20 ', fg="gray",
        background="#C6E2FF")
  self.efficiency.grid(row=9, column=1, rowspan=1)
  # Create input and output text boxes
  self.output_txt =Text(self, width=60, height=15, wrap=WORD,
        font='STXingkai -20 ', border=5, fg="gray")
  self.output_txt.grid(row=2, column=4, rowspan=5)
  # Create a scroll bar for the output text box
  self.scrollbar =Scrollbar(self)
  self.output_txt.configure(yscrollcommand=self.scrollbar.set)
  self.scrollbar['command']= self.output_txt.yview
  self.scrollbar.set(0.5,1)
  self.scrollbar.grid(row=3, column=5)
  self.scrollbar.grid_anchor(E)
 # Generate random numbers
 def create_rand_number(self):if self.choose.get()=="Please click the Generate Random Number button below":for i inrange(self.count):
    r = random.randint(-10000,10000)
    self.number.append(r)
   self.txt['text']="The random number has been successfully generated"
  elif self.choose.get()=="Please enter random numbers in the right dialog box, separated by spaces":
   self.txt['text']="Please enter random numbers in the right dialog box, separated by spaces"else:
   self.txt['text']="Please select the mode before proceeding to the next step"
 # exit the program
 def GuiExit(self):
  self.quit()
 def update_text(self):"""Update the value and text content of the radio button"""
  message =""
  message += self.choose.get()
  self.txt['text']= message
  if self.choose.get()=="Please enter random numbers in the right dialog box, separated by spaces":
   self.get_Text_number()
 # Get the number in the input and output text box
 def get_Text_number(self):try:
   string = self.output_txt.get(0.0,END)
   list = re.split(' ',string)
   flag =0for i in list :
    num =int(i)
    self.number.append(num)
    flag +=1
   self.count = flag
   self.txt['text']="Have successfully read the numbers in the text box"
  except:
   pass
 # put in order
 # Insertion sort
 def insert(self):iflen(self.choose.get())>10:
   start = datetime.datetime.now()
   self.number = self.my.insert_sort(self.number)
   end = datetime.datetime.now()
   self.get_effiency_writefile(start,end,name="Insertion sort",text="insert")
   self.write_number_output()else:
   self.txt['text']="Please select sort mode first"
 # Call hill sort
 def shell(self):iflen(self.choose.get())>10:
   start = datetime.datetime.now()
   self.number = self.my.shell_sort(self.number)
   end = datetime.datetime.now()
   self.get_effiency_writefile(start, end, name="Hill sort", text="shell")
   self.write_number_output()else:
   self.txt['text']="Please select sort mode first"
 # Call bubble sort
 def buddle(self):iflen(self.choose.get())>10:
   start = datetime.datetime.now()
   self.number = self.my.buddle_sort(self.number)
   end = datetime.datetime.now()
   self.get_effiency_writefile(start, end, name="Bubble sort", text="buddle")
   self.write_number_output()else:
   self.txt['text']="Please select sort mode first"
 # Call quick sort
 def quick(self):iflen(self.choose.get())>10:
   start = datetime.datetime.now()
   self.number = self.my.quick_sort(self.number,0,self.count-1)
   end = datetime.datetime.now()
   self.get_effiency_writefile(start, end, name="Quick sort", text="quick")
   self.write_number_output()else:
   self.txt['text']="Please select sort mode first"
 # Call selection sort
 def select(self):iflen(self.choose.get())>10:
   start = datetime.datetime.now()
   self.number = self.my.select_sort(self.number)
   end = datetime.datetime.now()
   self.get_effiency_writefile(start, end, name="Select sort", text="select")
   self.write_number_output()else:
   self.txt['text']="Please select sort mode first"
 # Call heap sort
 def heap(self):iflen(self.choose.get())>10:
   start = datetime.datetime.now()
   self.number = self.my.heap_sort(self.number)
   end = datetime.datetime.now()
   self.get_effiency_writefile(start, end, name="Select sort", text="heap")
   self.write_number_output()else:
   self.txt['text']="Please select sort mode first"
 # Write the sorted numbers into the input and output text boxes
 def write_number_output(self):
  temp =""for i in self.number:
   temp +=str(i)
   temp +=" "
  self.output_txt.delete(0.0,END)
  self.output_txt.insert(0.0,temp)
 # View the interface source code
 def view_gui(self):try:
   f =open("Text_File\\gui.txt",'r+')
   txt = f.read()
   self.output_txt.delete(0.0,END)
   self.output_txt.insert(0.0,txt)
  except:
   pass
 # View sorting source code
 def view_sort(self):try:
   f =open("Text_File\\sort.txt",'r+')
   txt = f.read()
   self.output_txt.delete(0.0,END)
   self.output_txt.insert(0.0,txt)
  except:
   pass
 def get_effiency_writefile(self,start,end,name,text):
  write = name +"Starting time:\n"
  write +=str(start)
  write +="\n"
  write +="End Time:\n"
  write +=str(end)
  write +="\n"
  write +="time cost:"
  write +=str(end-start)
  write +="\n"
  self.efficiency['text']= write
  try:
   f =open('Text_File\\%s_result.txt'%(text),'w')for i in self.number:
    write +=str(i)
    write +=" "
   f.write(write)
   f.close()
  except:
   pass
if __name__ =='__main__':
 root  =Tk()
 root.title("Data structure-sort")
 root.geometry("400x400")
 app =Application(root)
 root.iconbitmap("hanhan.ico")
 root.mainloop()

Sort code##

# - *- coding: utf-8-*-import math
import sys
classMy_Sort():
 #1 Insertion sort
 def insert_sort(self,lists):
  count =len(lists)for i inrange(1,count):
   key = lists[i]
   j = i-1while j>=0:if lists[j]> key:
     lists[j+1]= lists[j]
     lists[j]= key
    j-=1return lists
 #2 Hill sort,It must be noted in python that the step size in the loop is an integer
 def shell_sort(self,lists):
  count =int(len(lists))
  step =2
  group =int(count /step)while group >0:for i inrange(group):
    j = i+group
    while j< count:
     k = j - group
     key = lists[j]while(k>=0):if lists[k]>key:
       lists[k+group]= lists[k]
       lists[k]= key
      k -= group
     j += group
   group =int(group/step)return lists
 #3 Bubble Sort
 def buddle_sort(self,lists):
  count =len(lists)for i inrange(count):for j inrange(i+1,count):if lists[i]>lists[j]:
     lists[i],lists[j]= lists[j],lists[i]return lists
 #4 Direct selection sort
 def select_sort(self,lists):
  count =len(lists)for i inrange(count):
   min = i
   for j inrange(i+1,count):if lists[min]> lists[j]:
     min = j
   lists[min],lists[i]= lists[i],lists[min]return lists
 #5 Quick sort
 def quick_sort(self, lists, left, right):if left > right:return
  i = left
  j = right
  while i != j:while lists[j]>= lists[left] and i < j:
    j -=1while lists[i]<= lists[left] and i < j:
    i +=1if i < j:
    lists[i], lists[j]= lists[j], lists[i]
  lists[left], lists[i]= lists[i], lists[left]
  self.quick_sort(lists, left, i -1)
  self.quick_sort(lists, i +1, right)return lists
 #6 Heap sort
 # Tuning heap
 def adjust_heap(self,lists,i,size):
  lchild =2*i+1
  rchild =2*i+2
  max = i
  if i <int(size/2):if lchild < size and lists[lchild]> lists[max]:
    max = lchild
   if rchild < size and lists[rchild]> lists[max]:
    max = rchild
   if max !=i:
    lists[max],lists[i]= lists[i],lists[max]
    self.adjust_heap(lists,max,size)
 # Create heap
 def build_heap(self,lists,size):for i inrange(0,int((size/2)))[::-1]:
   self.adjust_heap(lists,i,size)
 # Heap sort
 def heap_sort(self,lists):
  size =int(len(lists))
  self.build_heap(lists,size)for i inrange(0,size)[::-1]:
   lists[0],lists[i]= lists[i], lists[0]
   self.adjust_heap(lists,0,i)return lists

Recommended Posts

Write gui in python
Python write Tetris
How to write win programs in python
Write a Qixi confession artifact in Python
How to write try statement in python
03. Operators in Python entry
Join function in Python
12. Network Programming in Python3
print statement in python
Python GUI interface programming
Concurrent requests in Python
Teach you how to write games in python
Context management in Python
Arithmetic operators in python
How to write a confession program in python
MongoDB usage in Python
Str string in Python
Computational Geometry in Python
Concurrent requests in Python (part 2)
Subscripts of tuples in Python
Talking about inheritance in Python
Noteworthy update points in Python 3.9
Python open read and write
Containerize Python applications in 3 minutes
What is introspection in python
Python GUI simulation implementation calculator
What is object-oriented in python
Generators and iterators in Python
Talking about strings in Python
Python write breakpoint download software
The usage of wheel in python
Summary of logarithm method in Python
Use of Pandas in Python development
Use nohup command instructions in python
Is there function overloading in python
How to write python configuration file
Getting started with Numpy in Python
Detailed sorting algorithm (implemented in Python)
How to wrap in python code
What does rc1 mean in python
Common errors and solutions in python
How to omit parentheses in Python
Detailed usage of dictionary in Python
Learn about garbage collection in Python
How to filter numbers in python
​What are the numbers in Python?
How to read Excel in Python
There are several keywords in Python
Everything in Python is an object
How to view errors in python
What does np do in python
Talking about the modules in Python
How to understand variables in Python
How to clear variables in python
The usage of tuples in python
How to use SQLite in Python
Actual combat | Python write port scanner
Description of in parameterization in python mysql
Is there an algorithm in python language
How to delete cache files in python
How to introduce third-party modules in Python