in tkinter how do i get a number into an entry

258 views Asked by At

Im a beginner in Python and running into the following problem:

for an assignment i need to get the number that gets inserted into text1, then when i push the button pk<<<kw multiply it by 1.36 and insert it into text2. and reverse by pressing the other button.

only i have no clue how.

from tkinter import *
root= Tk()
root.title("python programma omrekenen")
root.geometry("300x200")

def Iets():
   
    label = Label(text="vermogen in pk:")
    label.place(x=50,y=50)


    text1=IntVar()
    entry1=Entry(textvariable=text1)
    entry1.place(x=150,y=50)
    x1=text1.get()
    

    def Functie 10:

    def Functie 11:
    
    button=Button (text="PK>>>KW", command=Functie10)
    button.place (x=50,y=100)
    

    button=Button (text="PK<<<KW", command=functie11)
    button.place (x=150,y=150)
    


    label = Label(text="vermogen in KW:")
    label.place(x=50,y=150)


    text2=IntVar()
    entry2=Entry(textvariable=text2)
    entry2.place(x=150,y=150)
    x2=text2.get()


    
root.mainloop()

sorry for the bad english

2

There are 2 answers

3
Sandrin Joy On BEST ANSWER

command is used to call a function.

In your case , it does a multiplication on a number and gets inserted into an entry field.

Also , delete & insert are 2 tkinter methods clear & insert the data respectively

from tkinter import *
root= Tk()
root.title("python programma omrekenen")
root.geometry("300x400")
text1=IntVar()
text2=IntVar()
def first():
    entry2.delete(0,END)
    entry2.insert(0,text1.get()*1.36)
    
def second():
    entry1.delete(0,END)
    entry1.insert(0,text2.get()*1.36)   


label = Label(text="vermogen in pk:")
label.place(x=50,y=50)

entry1=Entry(textvariable=text1)
entry1.place(x=150,y=50)

button=Button (text="PK>>>KW", command=first)
button.place (x=50,y=100)

button=Button (text="PK<<<KW", command=second)
button.place (x=50,y=200)

label = Label(text="vermogen in KW:")
label.place(x=50,y=150)

entry2=Entry(textvariable=text2)
entry2.place(x=150,y=150)
root.mainloop()
0
PitPot_ PitPot_ On

That little code should hopefully help you out :

from tkinter import *

root = Tk()
root.title("python programma omrekenen")
root.geometry("300x200")


def Iets():

    Entry1 = Entry(root, bd=4)
    Entry1.grid(row=1, column=2)

    Entry1.delete(0, END)
    Entry1.insert(0, "Insert number")

    Entry2 = Entry(root, bd=4)
    Entry2.grid(row=2, column=2)

    Entry2.insert(0, "Result")

    def multiply(entry: int):
        if entry == 1:
            Entry2.delete(0, END)
            try:
                Entry2.insert(0, f"{int(Entry1.get()) * 2}")
            except ValueError:
                Entry2.insert(0, f"Invalid value")
            Entry1.delete(0, END)

        if entry == 2:
            Entry1.delete(0, END)
            try:
                Entry1.insert(0, f"{int(Entry2.get()) * 2}")
            except ValueError:
                Entry1.insert(0, f"Invalid value")
            Entry2.delete(0, END)

    Button1 = Button(root, text='Multiply', bd=2, bg='green', command=lambda: multiply(1))
    Button1.grid(row=1, column=1)

    Button2 = Button(root, text='Multiply', bd=2, bg='green', command=lambda: multiply(2))
    Button2.grid(row=2, column=1)


Iets()
root.mainloop()

Not sure if thats what you ment by reverse it but yeah.. its easy to understand so you should be fine !