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

2024-09-30 16:36:54 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

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

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

错误消息如下:

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

所以,在我看来,行中的输入

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

我真的不明白为什么。在过去的三天里,我试图解决这个问题,我在这里和其他地方读了几个小时的书,但我找不到答案(或者至少我不明白)。你知道吗

我非常感谢你的帮助,非常感谢

JD公司

ps:单击按钮将允许程序在命令提示符中执行其工作,而不是在gui框架中(显然忽略文本小部件)


Tags: text部件placebuttonrootrdframetk
2条回答

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

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

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

相关问题 更多 >