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.
I am trying to write a GUI for the first time and I have the following problem:
I wrote a program to find any given document on the c-drive, open/read text, count the characters and then display the result. 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. 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).
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)
发生的一个核心问题是 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()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.