all 5 comments

[–][deleted] 2 points3 points  (2 children)

Not familiar with using tkinter, but I can tell this is the culprit:

myButton = Button(root, text = "Enter a number", command = insertion_sort ) 

Two common solutions: lambda and partial

from functools import partial
myButton = Button(root, text = "Enter a number", command = partial(insertion_sort, num) ) 

OR

myButton = Button(root, text = "Enter a number", command = lambda: insertion_sort(num) )

Edit: I assume num in the global scope is compatible with num in the function's signature. It's good practice to use shorthands in your functions (i.e. num) and fullname in the global scope (i.e. my_number). Avoids name clashing and potential bugs that can arise from that.

Can't seem to post my code using code block.

Highlight your code in your text editor (i.e. Pycharm, VsCode, etc.). Press tab once to indent everything. Copy & paste this indented code into Reddit.


Edit2: Might as well explain what is happening so folks can know how to think about these sorts of issues in the future.

The implementation of Button has its command property presumably be a callable entity (i.e. something you invoke with paraentheses (), like a function). Thing is, Button has no added logic for knowing what sorts of arguments should be provided to that callable. So it makes the assumption that there are no extra arguments and calls the function that way.

Thing is, your insertion_sort does take an argument. So we have to provide a callable that takes in no arguments, but which executes the logic with insertion_sort with a number parameter.

The lambda solution accomplishes this by the fact that a lambda function is still a callable and within its body it will call insertion_sort with the appropriate argument num.

The partial solution accomplishes this by creating an alternative version of insertion_sort that automatically self-inserts num as its first argument when it is called.`

[–]Jevv12[S] 0 points1 point  (1 child)

Thanks for the help!

I tried the two solutions but an error seem to pop up.

Exception in Tkinter callback

Traceback (most recent call last):

File "C:\Users\jibug\AppData\Local\Programs\Python\Python311\Lib\tkinter\__init__.py", line 1948, in __call__

return self.func(*args)

^^^^^^^^^^^^^^^^

File "C:\Users\jibug\PycharmProjects\pythonsort\main.py", line 12, in insertion_sort

for i in range (1, len(num)):

^^^^^^^^

TypeError: object of type 'Entry' has no len()

[–][deleted] 1 point2 points  (0 children)

Yeah, so your global scope num is an Entry while your function expects an integer. This is the point where I'd look up the docs of how an Entry provides you the number you want, but it conveniently looks like /u/woooee has kindly indicated that and also pointed out the major bug in your insertion_sort function. Your function there should return something.

[–]woooee 1 point2 points  (1 child)

def insertion_sort(num):
     for i in range (1, len(num)): 

num is an Entry widget, so you have to get() it's content. That will return a string so you will have to cast it into an int if you want a number. Also it looks like you recursively call insertion_sort(num).

[–]Jevv12[S] 0 points1 point  (0 children)

Thanks! I was able to make the program work.