将函数结果传递给tkinter GUI文本小部件python3

将函数结果传递给tkinter GUI文本小部件python3,python,tkinter,Python,Tkinter,我是编程新手,如果这是一个明显的/微不足道的错误,我深表歉意。 我正在tkinter上编写一个GUI,在运行Windows1064bit的计算机上使用Python3.7.4(带Thonny和/或IDLE) 我第一次尝试编写GUI,遇到以下问题: 我编写了一个程序来查找c-drive上的任何给定文档,打开/读取文本,计算字符数,然后显示结果。 只要我使用tkinter框架小部件,它就可以正常工作,但是框架小部件不允许滚动条(我看到了一些解决方案,这些解决方案都在我的脑海中浮现),所以我决定使用文本

我是编程新手,如果这是一个明显的/微不足道的错误,我深表歉意。 我正在tkinter上编写一个GUI,在运行Windows1064bit的计算机上使用Python3.7.4(带Thonny和/或IDLE)

我第一次尝试编写GUI,遇到以下问题:

我编写了一个程序来查找c-drive上的任何给定文档,打开/读取文本,计算字符数,然后显示结果。 只要我使用tkinter框架小部件,它就可以正常工作,但是框架小部件不允许滚动条(我看到了一些解决方案,这些解决方案都在我的脑海中浮现),所以我决定使用文本小部件。有一行:

label2["text"] = a,"+", b, "+", c, "+", d, "=" , e 
由于某种原因,它不起作用,我不明白为什么它一开始就起作用,我不确定为什么它现在不起作用

为了进行一些测试,我创建了一个稍微容易测试的代码,但存在相同的问题:

import tkinter as tk
import random as rd

def fun9(d):
    a= rd.randint(1,10)
    b= rd.randint(1,10)
    c= rd.randint(1,10)
    #d= int(entry.get())
    e=a+b+c+d
    #label2["text"] = a,"+", b, "+", c, "+", d, "=" , e ### <-
    #label2.config(text = (a,"+", b, "+", c, "+", d, "=" , e))
    print(a,"+", b, "+", c, "+", d, "=" , e)
    return a,"+", b, "+", c, "+", d, "=" , e



root = tk.Tk() # root window?? -> opens window
root.geometry("%dx%d" % (800, 800))

# now write all functions
canvas = tk.Canvas(root) # create canvas for button
canvas.pack()

frame1 = tk.Frame(root, bg="#99ceff", bd=5)
frame1.place(relx=0.125, rely=0, relwidth=0.75, relheight=0.3)

label = tk.Label(frame1, font=20, text="here a, b, c and d are added: ")
label.place(relheight=1, relwidth=1)

frame2 = tk.Frame(root, bg="#99ceff", bd=5)
frame2.place(relx=0.5, rely=0.32, relwidth=0.75, relheight=0.1, anchor="n")

entry=tk.Entry(frame2, font=40, bd=5)# puts entry in frame instead of root
entry.place(relwidth=0.65, relheight=1)

button = tk.Button(root, text="Search and Display", bg="lightblue", font=40, command=lambda: fun9(int(entry.get()))) # creates button
button.place(relx=0.62, rely=0.32, relheight=0.1, relwidth=0.25) # places button in window

frame3 = tk.Frame(root, bg="#99ceff", bd=5)
frame3.place(relx=0.125, rely=0.45, relwidth=0.75, relheight=0.4)

#label2 = tk.Label(frame3, font=20)
#label2.place(relheight=1, relwidth=1)

label2 = tk.Text(frame3, font=20)
label2.pack()
label2.insert("1.0", fun9(int(entry.get())))
#label2.config(state="disabled")

root.mainloop()    
在我看来,行中的输入

label2.insert("1.0", fun9(int(entry.get())))
没有得到输入值,我真的不明白为什么。在过去的三天里,我一直试图解决这个问题,我在这里和其他地方读了几个小时的书,但我找不到答案(或者至少我不明白)

非常感谢您的帮助,非常感谢

法学博士


ps:点击按钮将允许程序在命令promt中完成它的工作,而不是在gui框架中(显然忽略文本小部件)

出现的一个核心问题是fun9()返回一个元组,然后您试图将该元组转换为int[line 68]。

这是我从朋友那里得到的答案,谁是一个软件开发人员,所以我不能因此而获得任何荣誉!不过,这可能会在将来帮助其他人,这就是我在这里发布它的原因:

import tkinter as tk
import random as rd

def fun9(result_textbox, d):
    a = rd.randint(1,10)
    b = rd.randint(1,10)
    c = rd.randint(1,10)
    e=a+b+c+d
    result = a,"+", b, "+", c, "+", d, "=" , e
    result_textbox.insert("1.0", result)
    print(result)
    return result



root = tk.Tk() 
root.geometry("%dx%d" % (800, 800))

# now write all functions
canvas = tk.Canvas(root) # create canvas for button
canvas.pack()

instruction_frame = tk.Frame(root, bg="#99ceff", bd=5)
instruction_frame.place(relx=0.125, rely=0, relwidth=0.75, relheight=0.3)

instruction_label = tk.Label(instruction_frame, font=20, text="here a, b, c and d are added: ")
instruction_label.place(relheight=1, relwidth=1)

entry_frame = tk.Frame(root, bg="#99ceff", bd=5)
entry_frame.place(relx=0.5, rely=0.32, relwidth=0.75, relheight=0.1, anchor="n")

entry=tk.Entry(entry_frame, font=40, bd=5)
entry.place(relwidth=0.65, relheight=1)

result_frame = tk.Frame(root, bg="#99ceff", bd=5)
result_frame.place(relx=0.125, rely=0.45, relwidth=0.75, relheight=0.4)

result_textbox = tk.Text(result_frame, font=20)

button = tk.Button(root, text="Search and Display", bg="lightblue", font=40, command=lambda: fun9(result_textbox, int(entry.get()))) # creates button
button.place(relx=0.62, rely=0.32, relheight=0.1, relwidth=0.25) # places button in window

result_textbox.pack()

root.mainloop()   

嘿,谢谢你的回答!您是否可以参考第36行?(还是第47行?)据我所知,代码只有50行
import tkinter as tk
import random as rd

def fun9(result_textbox, d):
    a = rd.randint(1,10)
    b = rd.randint(1,10)
    c = rd.randint(1,10)
    e=a+b+c+d
    result = a,"+", b, "+", c, "+", d, "=" , e
    result_textbox.insert("1.0", result)
    print(result)
    return result



root = tk.Tk() 
root.geometry("%dx%d" % (800, 800))

# now write all functions
canvas = tk.Canvas(root) # create canvas for button
canvas.pack()

instruction_frame = tk.Frame(root, bg="#99ceff", bd=5)
instruction_frame.place(relx=0.125, rely=0, relwidth=0.75, relheight=0.3)

instruction_label = tk.Label(instruction_frame, font=20, text="here a, b, c and d are added: ")
instruction_label.place(relheight=1, relwidth=1)

entry_frame = tk.Frame(root, bg="#99ceff", bd=5)
entry_frame.place(relx=0.5, rely=0.32, relwidth=0.75, relheight=0.1, anchor="n")

entry=tk.Entry(entry_frame, font=40, bd=5)
entry.place(relwidth=0.65, relheight=1)

result_frame = tk.Frame(root, bg="#99ceff", bd=5)
result_frame.place(relx=0.125, rely=0.45, relwidth=0.75, relheight=0.4)

result_textbox = tk.Text(result_frame, font=20)

button = tk.Button(root, text="Search and Display", bg="lightblue", font=40, command=lambda: fun9(result_textbox, int(entry.get()))) # creates button
button.place(relx=0.62, rely=0.32, relheight=0.1, relwidth=0.25) # places button in window

result_textbox.pack()

root.mainloop()