-2

I am trying to take entry from an entry boxes and use them in an equation. Basically it is

Entry_one - Entry_two * entry_three / 12. But I cant seem to figure out how to do this. To make it more clear here is the working python code:

a = int(input('Order Amount. '))
c = int(input('Beams Left. '))
l = int(input('Beam Length. '))

a1 = a-c
a2 = a1*l
a3 = a2/12

print ('The Hourly Count Is: ',a3)

Now here is the code that I'm trying to make do the exact same thing with tkinter:

from tkinter import *
root = Tk()
root.geometry('450x450')
root.title('Hourly Production Count')

def calc_result():
    subtract_var = int(total_order_ent.get()) - int(beams_left_ent.get())
    beams_left_var = int(subtract_var) * int(length_ent.get())
    order_output = int(beams_left_var) / 12
    answer_var = order_output.get()
    label = Label(root,text = answer_var).pack()
button1 = Button(root,text = 'Enter',command =    calc_result,fg='black',bg= 'green').pack()

total_order_ent = Entry(root).pack()
Beams_left_ent = Entry(root).pack()
length_ent = Entry(root).pack()

label_total = Label(root,text = 'Enter Total Order Amount').pack()
label_beams_left = Label(root,text = 'Enter Amount Left To Complete').pack()

root.mainloop()

That's what I have so far. I havn't used any grid placement for the widgets yet since I just want to get the code to work before I work on how it looks but if anyone can help me it would be appreciated. I've searched other questions and have modified other code, tried it as a class and other things and just cant seem to make it work. some errors I'm getting are:

line 23, in <module>
label_length = Label('Enter Beam Length').pack()
Python\Python35\lib\tkinter\__init__.py", line 2109, in _setup
self.tk = master.tk
AttributeError: 'str' object has no attribute 'tk'
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • 1
    There is no line -->*label_length = Label('Enter Beam Length').pack()*<-- in the code you posted. Do you have 2 programs with the same name in different directories? Also, pack() returns None so *total_order_ent.get()* will be None. –  May 31 '16 at 00:24
  • the error report you are showing is on line 23 but there is no line 23 in the code you provide... – Walle Cyril May 31 '16 at 00:25
  • right? that's what's so confusing about those errors too. I ran the code in the shell then those errors came up and I copied and pasted them into my question. There is no other code either. – king alfred May 31 '16 at 02:11
  • 1
    *"There is no other code..."* Yes. There is. It has at least 23 lines, and the 23rd line is `label_length = Label('Enter Beam Length').pack()`. The code you copied is not the code you are running. Did you move / rename the file outside of your editor? – SiHa May 31 '16 at 13:48
  • what I have posted is the entirety of the code. doesn't even take up enough space to be able to scroll down. Just ran it and this is the whole error message: – king alfred Jun 01 '16 at 17:19
  • Traceback (most recent call last): File "C:\Python\Python35\hourly2.py", line 23, in label_length = Label('Enter Beam Length').pack() File "C:\Users\nbrbn\Python\Python35\lib\tkinter\__init__.py", line 2605, in __init__ Widget.__init__(self, master, 'label', cnf, kw) File "C:\Users\nbrbn\Python\Python35\lib\tkinter\__init__.py", line 2131, in __init__BaseWidget._setup(self, master, cnf) File "C:\Users\nbrbn\Python\Python35\lib\tkinter\__init__.py", line 2109, in _setup self.tk = master.tk AttributeError: 'str' object has no attribute 'tk' – king alfred Jun 01 '16 at 17:24
  • How can the error be on line 23 of your code, when your code doesn't have 23 lines? Also, how can it be on a line that creates a label with the text "Enter Beam Length" when the code you posted has no label with that text? I think you're running different code than you think you are. It is _literally_ impossible for the code you posted to generate the error you say it is. – Bryan Oakley Jun 03 '16 at 10:35

2 Answers2

1

Your code doesn't exhibit the error you say it does. Just about the only possible way to get that specific error is if you omit the first three lines of your program after the import, and set root to a string. Even when you do that, you get a slightly different error -- it will complain about the first use of Button rather than Label as in your question.

My guess is, you're not running the exact code you think you are. Regardless, this specific problem is due to the fact you haven't properly created a root window, and you're passing a string as the parent to the other widgets.

Once you solve that problem, you also will need to solve the problem addressed in this question: Tkinter: AttributeError: NoneType object has no attribute get. The short version is, total_order_ent = Entry(root).pack() will set total_order_ent to None, making it impossible to get the value of the widget at a later date.

Community
  • 1
  • 1
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
0

you could create a variable and retrieve it with the get method in your function: as explained in the following documentation:

from tkinter import *

root = Tk()

var_01 = StringVar()

enter_01 = Entry(root, textvariable=var_01)
enter_01.pack()

def get_var():
    getvar = var_01.get()
    print(getvar)

button_01 = Button(root, text='print variable', command=get_var)
button_01.pack()

root.mainloop()
glls
  • 2,325
  • 1
  • 22
  • 39
  • while it is true that you _can_ create a variable, that's not the only solution. Your wording of "you need to..." is a bit too strong. – Bryan Oakley May 31 '16 at 00:51
  • Hmm. Wouldn't it need to be IntVar though? What I need is to be able to store the input in the Entry box as an integer then use those variables as part of an equation. Originally I did use textvariable=variable in Entry then made the variable = to IntVar assuming that would work but it didn't at all. I just want to be able to do exactly what that first bit of python code does but can't seem to figure out what is wrong with my code. – king alfred May 31 '16 at 02:28
  • you can convert the string to an Int or use intvar, if you do use intvar, and enter a non numerical char you will have to handle the error appropriately, since i dont see any error handling in your code, (or viceversa, if you try to convert chars to an int) – glls May 31 '16 at 02:29