0

Please help with my gui here is the code: I want to be able to have the user type in numbers in the entry field and be able to click add, then pressing thee sort button so that the SELECTION SORT sorts it.

title = "Selection Sort GUI"
A = [64, 25, 12, 22, 11]
# Traverse through all array elements
def sort(A):
    for i in range(len(A)):

        # Find the minimum element in remaining
        # unsorted array
        smallest = i
        for j in range(i+1, len(A)):
            if A[smallest] > A[j]:
                smallest = j

        # Swap the found minimum element with
        # the first element
        A[i], A[smallest] = A[smallest], A[i]

# Printing sorted array
print ("Sorted array:")
print (A)

master = tk.Tk()
master.title("Dans Selection Sort")
passwordEntry = tk.Entry(master)
passwordEntry.grid(row=0, column=1)
tk.Label(master, text='Enter Number: ', font='bold',).grid(row=0, column=0)
tk.Button(master, text='Add to Array', command=lambda: sort(A)).grid(row=1, column=0)
tk.Button(master, text='Sort', command=lambda: sort(A)).grid(row=1, column=1)
2
  • 3
    Can you be more specific about the type of help you need? What is this program doing, and how is it different from what you want? Feb 9, 2021 at 0:23
  • built-in function A = sorted(A) or in-place sorting A.sort()
    – furas
    Feb 9, 2021 at 2:43

2 Answers 2

0

As a starting point, I recommend a class based structure, stealing from https://stackoverflow.com/a/17470842/1942837

import tkinter as tk

class MainApplication(tk.Frame):
    def __init__(self, parent, *args, **kwargs):
        tk.Frame.__init__(self, parent, *args, **kwargs)
        self.parent = parent

        # init
        self.passwordEntry = tk.Entry(self)
        label = tk.Label(self, text='Enter Number: ', font='bold',)
        btn_add = tk.Button(self, text='Add to Array',
                            command=self.add_to_array)
        btn_sort = tk.Button(self, text='Sort',
                             command=self.sort)

        # layout
        self.passwordEntry.grid(row=0, column=1)
        label.grid(row=0, column=0)
        btn_add.grid(row=1, column=0)
        btn_sort.grid(row=1, column=1)

        # data
        self.values = list()

    def add_to_array(self):
        # called when the user presses the Add to Array button
        # (1) Fetch the Entry input:
        entry = self.passwordEntry.get()
        # (2) Add to our data list
        self.values.append(entry)
        print('values is now', self.values)

    def sort(self):
        # called when user presses Sort button
        # no need for special function, just use the inbuilt sorted()
        print('sorted', sorted(self.values))

if __name__ == "__main__":
    master = tk.Tk()
    master.title("Dans Selection Sort")
    MainApplication(master).pack(side="top", fill="both", expand=True)
    master.mainloop()

A key concept you missed is that you need to keep the array you want to sort somewhere, here as a list called self.values. I will leave it up to you to display this in the gui, here I just print it to the console. Instead of a custom sort function, I simply use the built-in sorted function.

As an alternative to a class would be to store the self.values as a global list and then have two functions that access that global list.

Let me know if any part of the above code is not clear.

0

There is already A = sorted(A) or in-place A.sort()

Rest needs only few changes. And maybe it needs better organization.

I use one Label to display array and other Label to display Sorted/Added

Button can send array to function using lambda but it can't get result - it would need to use global to assing result to external variable - so I skiped lambda and I access directly global array.


Minimal working code

import tkinter as tk

# --- classes ---

# ... empty ...

# --- functions ---

def sort():
    data.sort()  # built-in sorting `in-place`

    label_msg['text'] = "Sorted"
    label_result['text'] = str(data)

def add():
    data.append(int(entry.get()))

    label_msg['text'] = "Added"
    label_result['text'] = str(data)

# --- main ---

# - data - 

data = [64, 25, 12, 22, 11]

# - code - 

master = tk.Tk()

label = tk.Label(master, text='Enter Number: ')
label.grid(row=0, column=0)

entry = tk.Entry(master)
entry.grid(row=0, column=1)

button_add = tk.Button(master, text='Add to Array', command=add)
button_add.grid(row=1, column=0)

button_sort = tk.Button(master, text='Sort', command=sort)
button_sort.grid(row=1, column=1)

label_msg = tk.Label(master, text='')
label_msg.grid(row=2, column=0, columnspan=2)

label_result = tk.Label(master, text=str(data))
label_result.grid(row=3, column=0, columnspan=2)

master.mainloop()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.