Home > Software design >  Getting the value in Combobox as an integer
Getting the value in Combobox as an integer

Time:12-08

I'm working on a simple registration phase for a password scheme, but I can't seem to get the value in the combobox as an integer as it keeps giving ValueError: invalid literal for int() with base 10: ''. I've tried many methods but they don't seem to work. Additionally, I want to store the data in the entry and comboboxes for my next window, but it keeps registering as ValueError: '' is not in list.

The code:

import tkinter as tk
from tkinter import *
import random
from tkinter import messagebox
from tkinter import ttk
from tkinter.ttk import Combobox

root = tk.Tk()
root.geometry("375x130")
var = tk.StringVar()

data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

lbl = tk.Label(root, text = "Your password must have 7 to 15 characters with \nat least 1 number, 1 uppercase and 1 lowercase letter. ")
lbl1 = tk.Label(root, text = "Choose your password: ")
lbl2 = tk.Label(root, text = "n")
lbl3 = tk.Label(root, text = "y")
entry1 = tk.Entry(root)
combo1 = Combobox(root, values = data, state = "readonly")
combo2 = Combobox(root, values = data, state = "readonly")
lbl.place(x = 10, y = 0)
lbl1.place(x = 10, y = 35)
lbl2.place(x = 10, y = 60)
lbl3.place(x = 10, y = 85)
entry1.place(x = 155, y = 35)
combo1.place(x = 25, y = 60)
combo2.place(x = 25, y = 85)

pw = entry1.get()
n = combo1.get()
y = combo2.get()

root.title('Registration')
done_button = tk.Button(root, text = "Done", command = root.destroy, width = 5)
done_button.place(x = 205, y = 80)

root.mainloop()

password = str(pw)
n = int(data.index(n)) - 1
y = int(data.index(y)) - 1

CodePudding user response:

There are a couple of issues with your code:

  • Firstly, you need to bind the combobox change events with a function of some sort
  • The combobox returns a string of the number selected - which needs conversion
  • I am not sure what you aimed to do with n and y - but I kept the formula you used.

Fix these, and the combobox issues resolve.
See code below:

Code:

import tkinter as tk
from tkinter import *
import random
from tkinter import messagebox
from tkinter import ttk
from tkinter.ttk import Combobox

def combo_n_changed(event):
    n = combo1.get()
    n = int(data.index(int(n))) - 1
    print("n=",n)
    

def combo_y_changed(event):
    y = combo2.get()
    y = int(data.index(int(y))) - 1
    print("y=",y)
    
    
root = tk.Tk()
root.geometry("375x130")
var = tk.StringVar()

data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

lbl = tk.Label(root, text = "Your password must have 7 to 15 characters with \nat least 1 number, 1 uppercase and 1 lowercase letter. ")
lbl1 = tk.Label(root, text = "Choose your password: ")
lbl2 = tk.Label(root, text = "n")
lbl3 = tk.Label(root, text = "y")
entry1 = tk.Entry(root)
combo1 = Combobox(root, values = data, state = "readonly")
combo2 = Combobox(root, values = data, state = "readonly")
lbl.place(x = 10, y = 0)
lbl1.place(x = 10, y = 35)
lbl2.place(x = 10, y = 60)
lbl3.place(x = 10, y = 85)
entry1.place(x = 155, y = 35)
combo1.place(x = 25, y = 60)
combo2.place(x = 25, y = 85)

pw = entry1.get()

combo1.bind('<<ComboboxSelected>>', combo_n_changed)
combo2.bind('<<ComboboxSelected>>', combo_y_changed)

root.title('Registration')
done_button = tk.Button(root, text = "Done", command = root.destroy, width = 5)
done_button.place(x = 205, y = 80)

root.mainloop()


I entered the information as per the image below:

App image

Output:

n= 1
y= 2

CodePudding user response:

When the following lines are executed:

pw = entry1.get()
n = combo1.get()
y = combo2.get()

The entry box and the two comboboxes are just created, so nothing is input in the entry box and nothing is selected in the two comboboxes.

You need to do it inside a function triggered by the "Done" button:

...

# function called by "Done" button 
def done():
    pw = entry1.get()
    n = int(combo1.get()) # convert selection to integer
    y = int(combo2.get()) # convert selection to integer
    print(f"{pw=}, {n=}, {y=}")
    n = data.index(n) - 1
    y = data.index(y) - 1
    print(f"{n=}, {y=}")
    root.destroy()

root.title('Registration')
done_button = tk.Button(root, text = "Done", command = done, width = 5)
done_button.place(x = 205, y = 80)

root.mainloop()

enter image description here

Output in console:

pw='hello', n=4, y=9
n=2, y=7
  • Related