Introduction:
Hi all, I have zero experience in programming and am unsurprisingly having some difficulty with my first program...would be obliged if anyone could help me get over this hump in the road please.
Background to my issue:
I am having a problem calling integers from Tkinter input boxes. The setup is as follows, I have a frame with a descriptor label, an entry box, a confirm button and a confirmation label. The design I have in mind is for the user to enter a number, click on confirm button, which results in the input being stored and a confirmation label being updated. This part I am not having any issue with and works as hoped for....
from tkinter import *
root = Tk()
root.geometry('450x700+10+10')
root.title("Design Suite Version: 1.0")
#Creating a labelframe for my entry box/button and confirmation label
myframe2 = LabelFrame(root, text = '2: Limit state design selections:')
myframe2.grid(row=0, column=1, padx=5, pady=5, sticky = 'nw')
#Label identifying input I expect in my entry box...
Label(myframe2,
text="Proposed Member Depth:",
justify = LEFT).grid(row=21, column=0, padx=5, pady=5)
#Constructing the function for the self updating label
def Selection6():
#text expected in the confirmation label
labeltext6 = "Depth(mm) = " + str(var6.get())
#configuring the updating label
label6.config(text = labeltext6, fg='red', padx=5, pady=5)
#confirmation label location
label6 = Label(myframe2)
#confirmation label location
label6.grid(row=27, column=0, padx=0, pady=5)
#set what var is here
var6 = IntVar()
# initializing the choice
var6.set('0')
#this is my first entry box
entry6 = Entry(myframe2, textvariable=var6, width = 25)
entry6.grid(row=25, column=0, padx=5, pady=5)
#this is my first button, when clicked, runs Selection6
button = Button(myframe2, text="Click to Confirm",width = 20,
command = Selection6)
button.grid(row=26, column=0, padx=5, pady=0)
##########################################################################
#Creating a 2nd label for my entry box/button and confirmation label
Label(myframe2,
text="Proposed Member Width:",
justify = LEFT).grid(row=28, column=0,padx=5, pady=5)
def Selection6a():
labeltext6a = "Width(mm) = " + str(var6a.get())
label6a.config(text = labeltext6a, fg='red', padx=5, pady=5)
label6a = Label(myframe2)
label6a.grid(row=31, column=0, padx=0, pady=5)
var6a = IntVar()
var6a.set('0')
entry6a = Entry(myframe2, textvariable=var6a, width = 25)
entry6a.grid(row=29, column=0, padx=5, pady=5, sticky = 'w')
button = Button(myframe2, text="Click to Confirm",width = 20,
command = Selection6a)
button.grid(row=30, column=0, padx=5, pady=0)
mainloop()
Initial Issue:
Where I initially experienced difficulty was when I tried to get the variable inputs from my two entry boxes(above) and use them further on in my program (below) with my GUI via the addition of the following code...
##########################################################################
#Creating a 3rd label for my button and confirmation label
Label(myframe2,
text="Member Modulus:",
justify = LEFT).grid(row=32, column=0,padx=5, pady=5)
def Selection7():
labeltext7 = "Modulus(mm cubed) = ",
+ (((str(var6a.get()) * (str(var6.get()))**2))/6)
label7.config(text = labeltext7, fg='red', padx=5, pady=5)
label7 = Label(myframe2)
label7.grid(row=34, column=0, padx=0, pady=5)
var7 = IntVar()
var7.set('0')
button = Button(myframe2, text="Click for Modulus",width = 20,
command = Selection7)
button.grid(row=33, column=0, padx=5, pady=0)
mainloop()
When I try to get the two variables var6.get() and var6a.get() to use in a function to update a label, I am encountering an error as follows:
labeltext7 = "Modulus(mm cubed) = " + (((str(var6a.get()) * (str(var6.get()))**2))/6)TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int' Exception in Tkinter callback
Attempted Fix:
I have tried to convert the string to an integer by rewriting my function as follows....
def Selection7():
labeltext7 = "Modulus(mm cubed) = ",
+ (((int(var6a.get()) * (int(var6.get()))**2))/6)
label7.config(text = labeltext7, fg='red', padx=5, pady=5)
Current Issue:
When I call Selection7()
via button = Button(myframe2, text="Click for Modulus"
I am not getting the calculated value out to labeltext7
. The script now executes fine, without error, this is my issue, I can not get the calculation to output the function...any comments, recommendations or suggestions are welcome!
Some of the many posts which were suggested by the site which I have read but can not find my answer in:
How to get values from entry box with tkinter
tkinter set values in entry box
https://stackoverflow.com/questions/26935846/tkinter-entry-box-issues
Python posting answer from function to entry box in GUI
http://effbot.org/tkinterbook/entry.htm
UPDATE1:
Hi again, thanks for taking the time to comment and answer. I included a docstring to explain what I was hoping to achieve with my function, hopefully this adds some further clarity to the request.
I broke my compact code into smaller pieces, and also took out the comma as per recommendations.
Here is the revised portion of code, which I still can not get to output the desired function, any help appreciated.
Label(myframe2,
text="Member Modulus:",
justify = LEFT).grid(row=32, column=0,padx=5, pady=5)
"""
Selection 7()_Member Modulus:
Modulus for a rectangular section:
(Width of member x depth of member squared) divided by 6
width:var6a depth:var6
"""
def Selection7():
wdth = int(var6a.get())
dpth = int(var6.get())
dpthsqrd = int(dpth**2)
wdth_dpthsqrd = int((wdth * dpthsqrd))
mod = int(wdth_dpthsqrd/6)
labeltext7 = "Modulus (mm cubed) = "
+ int(mod)
label7.config(text = labeltext7, fg='red', padx=5, pady=5)
label7 = Label(myframe2)
label7.grid(row=34, column=0, padx=0, pady=5)
var7 = IntVar()
var7.set('0')
button = Button(myframe2, text="Click for Modulus",width = 20,
command = Selection7)
button.grid(row=33, column=0, padx=5, pady=0)
mainloop()
UPDATE1.1:
Hi all, in UPDATE 1, I should have expanded on what my issue was. The issue was that the calculation in defSelection7()
was not being displayed to the GUI at labeltext7
as hoped for...the text Modulus (mm cubed) =
was displaying to the GUI when Selection7
was activated, but the calculation int(mod)
was not.
UPDATE2:
On a happier note, my issue is resolved, I have managed to get the defSelection7()
to display the int(mod)
portion of labeltext7
by moving int(mod)
up a line and included a ,
.
Please see below for revised code.
"""
Selection 7()_Member Modulus:
Modulus for a rectangular section:
(Width of member x depth of member squared) divided by 6
width:var6a depth:var6
"""
def Selection7():
wdth = int(var6a.get())
dpth = int(var6.get())
dpthsqrd = (dpth**2)
wdth_dpthsqrd = (wdth * dpthsqrd)
mod = (wdth_dpthsqrd/6)
labeltext7 = "Modulus (mm cubed) = ", mod
label7.config(text = labeltext7, fg='red', padx=5, pady=5)
label7 = Label(myframe2)
label7.grid(row=34, column=0, padx=0, pady=5)
var7 = IntVar()
var7.set('0')
button = Button(myframe2, text="Click for Modulus",width = 20,
command = Selection7)
button.grid(row=33, column=0, padx=5, pady=0)
mainloop()
I am happy I have the GUI functioning as hoped for. I would be a little happier if someone could post a link for me to read which would help me identify why my revision works, I compacted labeltext7 = "Modulus (mm cubed) = "
+ int(mod)
from UPDATE1
to this UPDATE2 revision labeltext7 = "Modulus (mm cubed) = ", mod
and my GUI then updated as expected, pulling and displaying the calculation int(mod)
correctly to 'labeltext7'.