1

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'.

Billal Begueradj
  • 20,717
  • 43
  • 112
  • 130
Eoin
  • 21
  • 5
  • problems like this are much easier to debug if you don't try to do so much in a single line of code. Get the first variable in one statement, convert it in the next, use it in an expression in another, and add it to the label in another. This allows you to examine each intermediate value. There are no bonus points for compressing all that into a single line. – Bryan Oakley Jul 31 '15 at 01:55
  • Hi Bryan, thanks for the speedy reply and recommendation, fair point taken on board, I have expanded the portion of code which I am struggling with, and added a docstring to add some clarity and make it easier for me to get the help I need. – Eoin Jul 31 '15 at 12:24
  • So, what is it doing now? Displaying the wrong value? Crashing? Throwing an error? – Bryan Oakley Jul 31 '15 at 12:55
  • Hi Bryan, the code is not updating my `label7` as I had hoped. I had hoped to use the variables collected from `wdth = int(var6a.get())` and `dpth = int(var6.get())` to form `dpthsqrd = int(dpth**2)` and 'wdth_dpthsqrd = int((wdth * dpthsqrd))', to define `mod = int(wdth_dpthsqrd/6)`. I then hoped to use `labeltext7 = "Modulus (mm cubed) = " + int(mod)`, to display the result of the calculation above. The problem I am having is that the text in `labeltext7 = "Modulus (mm cubed) = " ` is displaying as planned, however the `+ int(mod)` portion of `labeltext7` is not outputting value to GUI. – Eoin Jul 31 '15 at 15:27

1 Answers1

0

Removing the comma should work:

def Selection7():
    labeltext7 = "Modulus(mm cubed) = "
    + (((int(var6a.get()) * (int(var6.get()))**2))/6)
    label7.config(text = labeltext7, fg='red', padx=5, pady=5)

Still better:

def Selection7():
    width = int(var6a.get())
    labeltext7 = "Modulus(mm cubed) = " + (width**3/6) 
    label7.config(text = labeltext7, fg='red', padx=5, pady=5)
Eric Levieil
  • 3,554
  • 2
  • 13
  • 18
  • Hi Eric, thank you for reviewing my request for assistance. I have updated my code to reflect the suggestions made, but am still unable to generate the required output. I have also included a docstring to add some clarity to what I am attempting to do. If you could review when you get a chance and suggest where I might be going wrong, then that would be appreciated. – Eoin Jul 31 '15 at 12:20
  • @Eoin: sorry, I was on holidays. Have you managed to solve your issue? If not, can you please post your full current code at the bottom of your question? – Eric Levieil Aug 09 '15 at 15:57
  • @Eoin: if I can add some general advices: don't use StringVar, IntVar... unless you have a strong reason to do so (usually a value that is used at several places) And give meaningful names to your variables and functions: Selection7 should be called computeModulus for example. – Eric Levieil Aug 09 '15 at 15:58
  • Hi Eric, no problem whatsoever am grateful for the assistance offered. You will be happy to hear that I solved my initial issue. I will take on board your advice about giving meaningful names to my variables and functions. Regarding the StringVar, IntVar usage, I am often calling the value associated with Str/IntVar more than once and re-using in my little program, but would be more than happy to read up some more about when to use/not use multiple instances of the Str/IntVar classes if you could forward me a link to a page, preferably with some examples for a beginner. Thanks very much. – Eoin Aug 11 '15 at 22:55