2015-10-14 22 views
-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() 

当我运行它时,它启动并创建GUI,但运行验证功能,而不必按下按钮,除了提交按钮之外,我没有引用它,为什么它激活?为什么使用tkinter调用此函数(该函数称为verification())?

+0

它不是我知道的最有效的代码,但我只是试图解决这个问题 –

回答

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

当该行执行,它会立即调用verification和任何验证返回绑定到鼠标点击事件。

将你的函数调用包装在一个lambda中,以防止它在程序开始时被调用。

submit_butt.bind("<Button-1>", lambda event: verifacation(oldB_ent.get(), newB_ent.get(), txt_ent.get())) 
相关问题