-1
#Defining all variables and lists
import string
from tkinter import ttk
from tkinter import *

#Inputing from the user the base, bast to be converted to and the number
def verifacation(oldB, newB, number):

    print("This has run")

    bases = ["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27"\
    ,"28","29","30","31","32","33"]
    allowed = ["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u"\
    ,"v","w","x","y","z"]

    if oldB not in bases or newB not in bases:
        display_lab.config(text="Please keep your bases to an acceptable whole number")
        return

    #Making sure the correct base was used for the number
    if [char for char in allowed[oldB:len(allowed)] if char in number] or [letter for letter in number if letter not in allowed]:
        display_lab.config(text="Please keep the number to the acceptable base")
        return

    #Changing the base of the number to denary
    number = int(number, oldB)

    #Calling the main function
    convert(number, newB)

#The one small block of code - Converts the denary number into base_converted
def convert(number, base_convert):
    while number > 0:
        #Getting the remainder of the number divided by the base, then dividing the number
        remainder = number % base_convert
        number = number // base_convert

        #Converting the number into a number or a letter
        remainder = allowed[remainder]

        converted += str(remainder)

    #Flipping the whole converted string (this is in the method) and displaying result to user
    converted = converted[::-1]
    display_lab.config(text=("This means:",converted, "in base:",base_convert))

#Creating A GUI
root = Tk()
root.title("Changing the base")

welcome_lab = Label(root)
welcome_lab.config(text="Welcome! \n This program coverts any base number into any other base (up to 32)!")
welcome_lab.grid(column=1, row=0)

cond_lab = Label(root)
cond_lab.config(text="Be warned, this program uses Crockford's Base32 version instead of the standard RFC 4648 version")
cond_lab.grid(column=1, row=1)

instruc_lab = Label(root)
instruc_lab.config(text=" Please enter the number you wish to convert (Keep it to the base you said the number is in) \n For the bases please select the appropriatte, from the dropdown menu")
instruc_lab.grid(column=1, row=2)

display_lab = Label(root)
display_lab.config(text="This is where all results will be displayed...")
display_lab.grid(column=1, row=6)

text_lab = Label(root)
text_lab.config(text="Number:")
text_lab.grid(column=0, row=3)

oldB_lab = Label(root)
oldB_lab.config(text="Old base:")
oldB_lab.grid(column=0, row=4)

newB_lab = Label(root)
newB_lab.config(text="New base:")
newB_lab.grid(column=0, row=5)

txt_ent = Entry(root)
txt_ent.config()
txt_ent.grid(column=1, row=3)

oldB_ent = ttk.Combobox(root)
oldB_ent.config(values = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33])
oldB_ent.grid(column=1, row=4)

newB_ent = ttk.Combobox(root)
newB_ent.config(values = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33])
newB_ent.grid(column=1, row=5)

submit_butt = Button(root)
submit_butt.config(text="Submit")
submit_butt.config(fg="green")
submit_butt.bind("<Button-1>", verifacation(oldB_ent.get(), newB_ent.get(), txt_ent.get()))
submit_butt.grid(column=0, row=0)

quit_butt = Button(root)
quit_butt.config(text="Exit")
quit_butt.config(fg="red", width=len(submit_butt["text"])-1, command=root.destroy)
quit_butt.grid(column=0, row=1)

root.mainloop()

When I run it it starts up and the code, creates the GUI but runs the verification function without me having to press a button, I haven't referenced it anywhere but in the submit button, why is it activating?

Frostyfeet909
  • 135
  • 1
  • 2
  • 16

1 Answers1

1
submit_butt.bind("<Button-1>", verifacation(oldB_ent.get(), newB_ent.get(), txt_ent.get()))

When this line executes, it immediately calls verification and whatever verification returns is bound to the mouse click event.

Wrap your function call in a lambda to prevent it from being called at the beginning of your program.

submit_butt.bind("<Button-1>", lambda event: verifacation(oldB_ent.get(), newB_ent.get(), txt_ent.get()))
Kevin
  • 74,910
  • 12
  • 133
  • 166