简体   繁体   English

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

[英]Pass function result to tkinter GUI Text widget python3

I am new to programming and I apologize if this is an obvious/trivial mistake.我是编程新手,如果这是一个明显/微不足道的错误,我深表歉意。 I am writing a GUI on tkinter using python 3.7.4 (with Thonny and/IDLE) on a computer running windows 10 64bit.我正在运行 Windows 10 64 位的计算机上使用 python 3.7.4(带有 Thonny 和/IDLE)在 tkinter 上编写 GUI。

I am trying to write a GUI for the first time and I have the following problem:我是第一次尝试编写 GUI,但遇到以下问题:

I wrote a program to find any given document on the c-drive, open/read text, count the characters and then display the result.我编写了一个程序来查找 C 驱动器上的任何给定文档,打开/读取文本,计算字符数,然后显示结果。 This works fine as long as I use the tkinter Frame widget but the frame widget does not allow for a scrollbar (i saw a few solutions for this that went right over my head) so i decided to use a text widget.只要我使用 tkinter 框架小部件,这就可以正常工作,但框架小部件不允许滚动条(我看到了一些解决方案),所以我决定使用文本小部件。 there the line:有一行:

label2["text"] = a,"+", b, "+", c, "+", d, "=" , e 

does not work for some reason and as I don't understand why it works in the first place I am not sure why it doesn't work now.由于某种原因不起作用,因为我不明白为什么它首先起作用,我不确定为什么它现在不起作用。

To make some tests I created a slightly easier to test code with the same problems:为了进行一些测试,我创建了一个稍微更容易测试具有相同问题的代码:

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

the error message reads:错误信息如下:

ValueError: invalid literal for int() with base 10: ''

so, as far as I see it, the input in the line所以,据我所知,行中的输入

label2.insert("1.0", fun9(int(entry.get())))

doesn't pick up the entry value and I really don't understand why.没有获取入口值,我真的不明白为什么。 I've tried to solve it for the last 3 days and I've read hours here and other places but I cannot find an answer (or at least not one I understand).在过去的 3 天里,我一直试图解决它,并且我已经在这里和其他地方阅读了数小时,但我找不到答案(或者至少不是我理解的答案)。

I appreciate any help and thank you very much我感谢任何帮助,非常感谢你

JD京东

ps: clicking the button will allow the programm to do its job in the command promt, just not in the gui frame (obviously ignoring the text widget) ps:单击按钮将允许程序在命令提示符中执行其工作,而不是在 gui 框架中(显然忽略文本小部件)

发生的一个核心问题是 fun9() 返回一个元组,然后您尝试将该元组转换为 int [第 68 行]。

this is the answer i got from a friend, who is a software developer, so i can't take any credit for it!这是我从一个软件开发者朋友那里得到的答案,所以我不能相信它! still maybe this will help someone else in the future, which is why i am posting it here:也许这将来会对其他人有所帮助,这就是我在这里发布的原因:

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

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

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