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

in tkinter how do i get a number into an entry

我是 Python 和 运行 的初学者,遇到以下问题:

对于一项作业,我需要获取插入到 text1 中的数字,然后当我按下按钮 pk<<

只是我不知道如何。

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

抱歉英语不好

command用于调用函数

在您的例子中,它对数字进行乘法运算并插入到输入字段中。

此外,delete & insert 是 2 个 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()

这个小代码应该能帮到你:

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

不确定这是否是您通过反转它所表达的意思,但是是的..它很容易理解,所以您应该没问题!