为了账号安全,请及时绑定邮箱和手机立即绑定

如何通过tkinter输入框将多个值从一个函数返回到另一个?

如何通过tkinter输入框将多个值从一个函数返回到另一个?

长风秋雁 2022-07-05 15:49:58
我对 tkinter 和 python 真的很陌生,我尝试使用 tkinter 创建 GUI。该示例只是计算两个数字的乘法和加法并返回,当我无法将字符串转换为输入时(这意味着没有输入),我一直无法生成结果。那么,我怎样才能将这 2 个值返回给另一个函数呢?import tkinter as tk'''OS'''root = tk.Tk()root.title("Simple Box")operator=""canvas = tk.Canvas(root, height=400,width=500,bg="White")canvas.pack()frame=tk.Frame(root, bg="Light Blue")frame.place(relwidth=0.9,relheight=0.9, relx=0.05, rely=0.05)'''Variable '''num1= tk.StringVar()num2= tk.StringVar()''' function'''def operation1():    global number1, number2    multi_add =tk.Tk()    multi_add.title("Multiplication and Addition")    tk.Label(multi_add, font =("Helvetica", 20),             text="Enter the first number:").grid(row=0)    tk.Label(multi_add, font =("Helvetica", 20),             text="Enter the second number").grid(row=1)    number1=tk.Entry(multi_add, textvariable = num1).grid(row=0, column=1)    number2=tk.Entry(multi_add, textvariable = num2).grid(row=1, column=1)    return num1, num2 def result():    num3 = float(num1.get()) + float(num2.get())    num4 = float(num1.get()) * float(num2.get())    print("the result is ",num3 ,"and", num4)'''buttons'''result=tk.Button(frame, text="Result", font=('airal', 30 ,'bold'), height="1"                 ,width="6", padx=10,pady=5, fg="Black",                 bg="yellow", command= result)result.place(x=300 , y=305)op1=tk.Button(frame, text="operation 1", height="2", width="10", padx=10,             pady=5, fg="Black", bg="yellow", command = operation1)op1.place(x = 17, y = 310)root.mainloop()追溯错误:D Tkinter 回调中的异常:Traceback(最近一次调用最后一次):文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/tkinter/init .py ”,第 1705 行,调用返回 self。 func(*args) 文件“/Users/Pozzphut/Desktop/Project Python/FigureFunction transfer.py”,第 37 行,结果 num3 = float(num1.get()) + float(num2.get()) ValueError: could不将字符串转换为浮点数:
查看完整描述

1 回答

?
12345678_0001

TA贡献1802条经验 获得超5个赞

这是解决方案:


如果您.grid()在同一行中使用number1,number2则将返回None,我们无法获取数据。


所做的更改如下:


    number1=tk.Entry(multi_add, textvariable = num1)

    number2=tk.Entry(multi_add, textvariable = num2)

    number1.grid(row=0, column=1)

    number2.grid(row=1, column=1)


def result():

    num3 = float(number1.get()) + float(number2.get())

    num4 = float(number1.get()) * float(number2.get())

这是整个代码:


import tkinter as tk


'''OS'''

root = tk.Tk()

root.title("Simple Box")

operator=""


canvas = tk.Canvas(root, height=400,width=500,bg="White")

canvas.pack()


frame=tk.Frame(root, bg="Light Blue")

frame.place(relwidth=0.9,relheight=0.9, relx=0.05, rely=0.05)

'''Variable '''

num1= tk.StringVar()

num2= tk.StringVar()


''' function'''

def operation1():

    global number1, number2

    multi_add =tk.Tk()

    multi_add.title("Multiplication and Addition")


    tk.Label(multi_add, font =("Helvetica", 20),

             text="Enter the first number:").grid(row=0)

    tk.Label(multi_add, font =("Helvetica", 20),

             text="Enter the second number").grid(row=1)


    number1=tk.Entry(multi_add, textvariable = num1)

    number2=tk.Entry(multi_add, textvariable = num2)

    number1.grid(row=0, column=1)

    number2.grid(row=1, column=1)


def result():

    num3 = float(number1.get()) + float(number2.get())

    num4 = float(number1.get()) * float(number2.get())

    print("the result is ",num3 ,"and", num4)


'''buttons'''

result=tk.Button(frame, text="Result", font=('airal', 30 ,'bold'), height="1"

                 ,width="6", padx=10,pady=5, fg="Black",

                 bg="yellow", command= result)

result.place(x=300 , y=305)


op1=tk.Button(frame, text="operation 1", height="2", width="10", padx=10,

             pady=5, fg="Black", bg="yellow", command = operation1)

op1.place(x = 17, y = 310)



root.mainloop()

和第二种方法


无需声明变量num1和num2:


import tkinter as tk


'''OS'''

root = tk.Tk()

root.title("Simple Box")

operator=""


canvas = tk.Canvas(root, height=400,width=500,bg="White")

canvas.pack()


frame=tk.Frame(root, bg="Light Blue")

frame.place(relwidth=0.9,relheight=0.9, relx=0.05, rely=0.05)


''' function'''

def operation1():

    global number1, number2

    multi_add =tk.Tk()

    multi_add.title("Multiplication and Addition")


    tk.Label(multi_add, font =("Helvetica", 20),

             text="Enter the first number:").grid(row=0)

    tk.Label(multi_add, font =("Helvetica", 20),

             text="Enter the second number").grid(row=1)


    number1=tk.Entry(multi_add)

    number2=tk.Entry(multi_add)

    number1.grid(row=0, column=1)

    number2.grid(row=1, column=1)


def result():

    num3 = float(number1.get()) + float(number2.get())

    num4 = float(number1.get()) * float(number2.get())

    print("the result is ",num3 ,"and", num4)


'''buttons'''

result=tk.Button(frame, text="Result", font=('airal', 30 ,'bold'), height="1"

                 ,width="6", padx=10,pady=5, fg="Black",

                 bg="yellow", command= result)

result.place(x=300 , y=305)


op1=tk.Button(frame, text="operation 1", height="2", width="10", padx=10,

             pady=5, fg="Black", bg="yellow", command = operation1)

op1.place(x = 17, y = 310)



root.mainloop()



查看完整回答
反对 回复 2022-07-05
  • 1 回答
  • 0 关注
  • 149 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信