简体   繁体   English

在 tkinter 中,我如何将数字输入到条目中

[英]in tkinter how do i get a number into an entry

Im a beginner in Python and running into the following problem:我是 Python 初学者,遇到以下问题:

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.对于作业,我需要获取插入 text1 的数字,然后当我按下按钮 pk<<<kw 将其乘以 1.36 并将其插入 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抱歉英语不好

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 !不确定这是否是您通过反转它所提到的,但是是的..它很容易理解,所以您应该没问题!

command is used to call a function. command用于调用函数。

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此外, deleteinsert是两种 tkinter 方法分别清除和插入数据

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM