tkinter Application object has no attribute for - python

i'm kinda new tkinter and i ran into a problem while using tkinter with python. I'm trying to get all of the buttons i have right now to take a number and add to it if the button is clicked or not, i ran into a wall and i have no idea how to fix it. Here's my code for reference.
from tkinter import *
class Application(Frame):
def __init__(self, master):
super(Application, self).__init__(master)
self.grid()
self.create_widgets()
def create_widgets(self):
Label(self,
text = "Enter information for services you need on your car"
).grid(row = 0, column = 0, columnspan = 2, sticky = W)
#Oil Change
Label(self,
text = "Oil Change"
).grid(row = 2, column = 0, sticky = W)
self.is_itchy = BooleanVar()
Checkbutton(self,
text = "$26.00",
#error here
variable = self.oil
).grid(row = 2, column = 1, sticky = W)
#Lube Job
Label(self,
text = "Lube Job"
).grid(row = 3, column = 0, sticky = W)
self.is_itchy = BooleanVar()
Checkbutton(self,
text = "$18.00",
variable = self.is_itchy
).grid(row = 3, column = 1, sticky = W)
#Radiator Flush
Label(self,
text = "Radiator Flush"
).grid(row = 4, column = 0, sticky = W)
self.is_itchy = BooleanVar()
Checkbutton(self,
text = "$30.00",
variable = self.is_itchy
).grid(row = 4, column = 1, sticky = W)
#Transmission Flush
Label(self,
text = "Oil Change"
).grid(row = 5, column = 0, sticky = W)
self.is_itchy = BooleanVar()
Checkbutton(self,
text = "$80.00",
variable = self.is_itchy
).grid(row = 5, column = 1, sticky = W)
#Inspection
Label(self,
text = "Inspection"
).grid(row = 6, column = 0, sticky = W)
self.is_itchy = BooleanVar()
Checkbutton(self,
text = "$15.00",
variable = self.is_itchy
).grid(row = 6, column = 1, sticky = W)
#Muffler Replacement
Label(self,
text = "Muffler Replacement"
).grid(row = 7, column = 0, sticky = W)
self.is_itchy = BooleanVar()
Checkbutton(self,
text = "$100.00",
variable = self.is_itchy
).grid(row = 7, column = 1, sticky = W)
#Tire Rotation
Label(self,
text = "Tire Rotation"
).grid(row = 8, column = 0, sticky = W)
self.is_itchy = BooleanVar()
Checkbutton(self,
text = "$20.00",
variable = self.is_itchy
).grid(row = 8, column = 1, sticky = W)
#Buttons
Button(self,
text = "Click for total price",
command = self.tell_story
).grid(row = 9, column = 0, sticky = W)
self.story_txt = Text(self, width = 35, height = 5, wrap = WORD)
self.story_txt.grid(row = 10, column = 0, columnspan = 3)
Button(self,
text = "Quit",
command = quit
).grid(row = 9, column = 1, sticky = W)
def tell_story(self):
""" Fill text box with new story based on user input. """
# get values from the GUI
if self.oil.get():
print("Goofus")
# create the story
story = Price
# display the story
self.story_txt.delete(0.0, END)
self.story_txt.insert(0.0, story)
root = Tk()
root.title("Joe's repair shop")
app = Application(root)
root.mainloop()
Here is the error i am getting
Traceback (most recent call last):
File "C:\Users\Kevin Holstein\Desktop\Classes\Python\Labs\Lab 10\Repair shop
kholstein.py", line 127, in <module>
app = Application(root)
File "C:\Users\Kevin Holstein\Desktop\Classes\Python\Labs\Lab 10\Repair shop
kholstein.py", line 8, in __init__
self.create_widgets()
File "C:\Users\Kevin Holstein\Desktop\Classes\Python\Labs\Lab 10\Repair shop
kholstein.py", line 24, in create_widgets
variable = self.oil
AttributeError: 'Application' object has no attribute 'oil'

On this line:
Checkbutton(self, text = "$26.00", variable = self.oil).grid(row = 2, column = 1, sticky = W)
You declare that the variable attribute of the Checkbutton widget should be equal to self.oil which you never give a value to, this throws an error as tkinter is trying to assign something which doesn't exist to this attribute.

Related

Tkinter window not responding after Text Insert

I'm new to Python and just started venturing into doing GUIs. I created a Tkinter window that is pretty basic: it has 3 Entry bars and 3 File Dialog buttons. When you chose the 3rd directory, the GUI file automatically makes a call to a separate file and receives a large text block which is then displayed in a Text box.
The whole thing works correctly, but my problem is that after receiving and inserting the text response, Tkinter stops working and doesn't allow the user to scroll down.
I read that one reason this happens is because people use both .pack( ) and .grid( ), but I'm not mixing those two functions.
Thanks in advance for any help!
Here's my GUI file
from tkinter import *
from tkinter import filedialog
from gui_GPSExtractor import *
import os
class Application(Frame):
def __init__(self, master):
""" Initialize Frame """
Frame.__init__(self, master)
self.grid( )
self.startGUI( )
""" Create Labels, Text Boxes, File Dialogs, and Buttons """
def startGUI(self):
# Label for Scan Path
self.dLabel = Label(self, text = "Scan Path")
self.dLabel.grid(row = 0, column = 0, columnspan = 2, sticky = W)
# Entry for Scan Path
self.dEntry = Entry(self, width = 60)
self.dEntry.grid(row = 1, column = 0, sticky = W)
# Button for Scan Path Directory Browse
self.dButton = Button(self, text = "Browse", command = lambda: self.browseFiles("d"))
self.dButton.grid(row = 1, column = 1, sticky = W)
# Label for CSV Path
self.cLabel = Label(self, text = "CSV Path")
self.cLabel.grid(row = 3, column = 0, columnspan = 2, sticky = W)
# Entry for CSV Path
self.cEntry = Entry(self, width = 60)
self.cEntry.grid(row = 4, column = 0, sticky = W)
# Button for CSV Path Directory Browse
self.cButton = Button(self, text = "Browse", command = lambda: self.browseFiles("c"))
self.cButton.grid(row = 4, column = 1, sticky = W)
# Label for Log Path
self.lLabel = Label(self, text = "Log Path")
self.lLabel.grid(row = 6, column = 0, columnspan = 2, sticky = W)
# Entry for Log Path
self.lEntry = Entry(self, width = 60)
self.lEntry.grid(row = 7, column = 0, sticky = W)
# Button for Log Path Directory Browse
self.lButton = Button(self, text = "Browse", command = lambda: self.browseFiles("l"))
self.lButton.grid(row = 7, column = 1, sticky = W)
# Text Box for Results
self.resultText = Text(self, width = 60, height = 30, wrap = WORD, borderwidth = 3, relief = SUNKEN)
self.resultText.grid(row = 9, column = 0, columnspan = 2, sticky = "nsew")
# Scrollbar for Text Box
self.scrollBar = Scrollbar(self, command = self.resultText.yview)
self.scrollBar.grid(row = 9, column = 2, sticky = "nsew")
self.resultText["yscrollcommand"] = self.scrollBar.set
def browseFiles(self, btnCalling):
if(btnCalling == "d"):
self.dName = filedialog.askdirectory(initialdir = "/python3-CH05")
self.dEntry.delete(0, END)
self.dEntry.insert(0, self.dName)
elif(btnCalling == "c"):
self.cName = filedialog.askdirectory(initialdir = "/python3-CH05")
self.cEntry.delete(0, END)
self.cEntry.insert(0, self.cName)
elif(btnCalling == "l"):
self.lName = filedialog.askdirectory(initialdir = "/python3-CH05")
self.lEntry.delete(0, END)
self.lEntry.insert(0, self.lName)
output = extractGPS(self.dName, self.cName, self.lName)
self.resultText.delete(0.0, END)
self.resultText.insert(0.0, output)
# Start the GUI
root = Tk( )
root.title("Python gpsExtractor")
root.geometry("650x650")
app = Application(root)
root.mainloop( )

how to return a value to the main function after clicking a button in tkinter?

In this file, I tried to return the value of employNum and employPass to the main function each time I clicked the display button. How can I do that?
from tkinter import *
def displayButton(root,employNum, employPass):
Label(root,text = employNum.get() ).grid(row = 3, column = 1, sticky = N+S+W+E)
Label(root, text = employPass.get()).grid(row = 4, column = 1, sticky = N+S+W+E)
def main():
root = Tk()
Label(root, text = 'Employee Number: ').grid(row = 0, column = 0, sticky = W)
Label(root, text = 'Login Password: ').grid(row = 1, column = 0, sticky = W)
employeeNum = StringVar()
employeePass = StringVar()
Entry(root, textvariable = employeeNum).grid(row = 0, column = 1, columnspan = 2, sticky = W)
Entry(root, textvariable = employeePass).grid(row = 1, column = 1, columnspan = 2, sticky = W)
checkButton = BooleanVar()
Checkbutton(root, text = 'Remember Me', variable = checkButton).grid(row = 2, column = 1, sticky = W)
Button(root, text = 'Save', relief = RAISED).grid(row = 2, column = 2, sticky = E)
display = Button(root, text = 'Display', relief = RAISED, command = lambda: displayButton(root, employeeNum,employeePass))
display.grid(row = 3, column = 2, sticky = E)
Label(root, text = "Employee's number is ").grid(row = 3, column = 0, sticky = W)
Label(root, text = "Employee's Passowrd is ").grid(row =4 , column = 0, sticky = W)
root.mainloop()
main()
Button can't return value using return. You can only set value in global variable or passed as argument. You can change text in Label assigned to global variable or passed as argument. You can create new Label but root have to be global variable or passed as argument.

Tkinter: global name not defined

I'm trying to add items to the listbox, but every time I try it, it's saying that the global name "title_input" is not defined. I don't understand why this doesn't work, because the last one that I did with this exact same structure didn't give me that error. I'm new to this, and the other tutorials and questions I read about global name errors didn't make sense to me. Thanks for the help!
from Tkinter import *
class Application(Frame):
def __init__(self, master):
Frame.__init__(self, master)
self.grid()
self.create_widgets()
self.list = Listbox(self, selectmode=BROWSE)
self.list.grid(row = 1, column = 4, rowspan = 10, columnspan = 3, sticky = W, padx = 5, pady = 5)
def create_widgets(self):
#setlist box - may not stay text box?
self.setlist = Text(self, height = 14, width = 25)
self.setlist.grid(row = 1, column = 0, rowspan = 10, columnspan = 3, sticky = W, padx = 5, pady = 5)
self.setlistLabel = Label(self, text = "SetList")
self.setlistLabel.grid(row = 0, column = 1, sticky = W, padx = 5, pady = 5)
#Library label
self.libraryLabel = Label(self, text = "Library")
self.libraryLabel.grid(row = 0, column = 5, sticky = W, padx = 5, pady y = 5)
#Library button/input
self.add_title = Button(self, text = "Add Title", command = self.add_item)
self.add_title.grid(row = 16, column = 5, sticky = W, padx = 5, pady = 5)
self.title_input = Entry(self)
self.title_input.grid(row = 16, column = 4, sticky = W, padx = 5, pady = 5)
def add_item(self):
list.insert(END, title_input.get())
def get_list(event):
index = list.curselection()[0]
seltext = list.get(index)
setlist.insert(0, seltext)
root = Tk()
root.title("SetList Creator")
root.geometry("500x500")
app = Application (root)
root.mainloop()
title_input is defined in your instance namespace but it is not defined in your global namespace. When you reference an unqualified title_input in your class method add_item, Python looks for title_input in the global namespace. When it doesn't find it there it gives you that error. Adding the self qualifier, self.title_input, to indicate that you wish to reference title_input in the instance namespace, will resolve the error.

Tkinter button not working (Python 3.x)

I'm working on my final project for my computing I class.
The problem that I am having is:
When I click on the new entry button, hit the back button and click on the new entry button once again it does not work.
If you guys could tell me why that is?
The command on the button seems to be only working once. Thanks for your help.
Code:
from tkinter import *
import tkinter.filedialog
class App(Tk):
def __init__(self):
Tk.__init__(self)
self.title("Entry Sheet")
self.font = ("Helvetica","13")
self.header_font = ("Helvetica","18")
self.exercise_font = ("Helvetica","13","bold")
self.delete = 'a'
self.new_user()
def new_user(self):
if self.delete == 'b':
self.delete = 'c'
self.hide()
self.delete = 'b'
self.new_entry = Button(self, text = 'New Entry', command = self.entry, width = 15)
self.new_entry.grid(row = 1, column = 0, columnspan = 3, padx = 10, pady = 5)
self.look_entry = Button(self, text = 'See Entries', command = self.see_entries, width = 15)
self.look_entry.grid(row = 2, column =0, columnspan = 3, padx = 10, pady = 5)
def entry(self):
print(1)
self.delete = 'b'
self.hide()
self.entry = Label(self, text = 'New Entry', font = self.header_font)
self.entry.grid(row = 0, column = 0, columnspan = 2)
self.numberlbl = Label(self, text = 'Please choose a muscle?', font = self.font)
self.numberlbl.grid(row = 1, column= 0, columnspan = 2, sticky = 'w' )
self.muscle_chosen = IntVar()
self.chest = Radiobutton(self, text = "chest", variable = self.muscle_chosen, value = 1, font = self.font)
self.bicep = Radiobutton(self, text = "bicep", variable = self.muscle_chosen, value = 2, font = self.font)
self.chest.grid(row = 2, column = 0)
self.bicep.grid(row = 2, column = 1)
self.exerciseslbl = Label(self, text = 'Please enter the number of exercises: ', font = self.font)
self.exerciseslbl.grid(row = 3, column = 0, columnspan = 3)
self.exercises_spinbox = Spinbox(self, from_= 1, to_= 50, width = 5, font = self.font)
self.exercises_spinbox.grid(row = 4, column = 0)
self.back_button = Button(self, text = 'Back', command = self.new_user, width = 10)
self.back_button.grid(row =5, column=0, pady =10)
def see_entries(self):
print("Goes through")
def hide(self):
if self.delete == 'b':
self.new_entry.grid_remove()
self.look_entry.grid_remove()
elif self.delete == 'c':
self.entry.grid_remove()
self.numberlbl.grid_remove()
self.chest.grid_remove()
self.bicep.grid_remove()
self.exerciseslbl.grid_remove()
self.exercises_spinbox.grid_remove()
self.back_button.grid_remove()
def main():
app = App()
app.mainloop()
if __name__=="__main__":
main()
In your entry function you overwrite self.entry, which is the name of the function, with a reference to a Label. When the button then calls self.entry it isn't function.
Simply call the Label something else.

Tkinter: Getting an image above buttons in grid layout

i'm new to tkinter and I was trying to make a GUI where there was an image at the top with an area of 4 buttons underneath that image which would be a method of selecting answers. However with the code I have so far the buttons that I create just seem to stay in the top left corner and will not move under the image at all, does anybody know a solution to this please?
import Tkinter as tk
from Tkinter import *
from Tkinter import PhotoImage
root = Tk()
class Class1(Frame):
def __init__(self, master):
Frame.__init__(self, master)
self.grid()
self.master = master
self.question1_UI()
def question1_UI(self):
self.master.title("GUI")
gif1 = PhotoImage(file = 'Image.gif')
label1 = Label(image=gif1)
label1.image = gif1
label1.grid(row=1, column = 0, columnspan = 2, sticky=NW)
questionAButton = Button(self, text='Submit',font=('MS', 8,'bold'))
questionAButton.grid(row = 2, column = 1, sticky = S)
questionBButton = Button(self, text='Submit',font=('MS', 8,'bold'))
questionBButton.grid(row = 2, column = 2, sticky = S)
questionCButton = Button(self, text='Submit',font=('MS', 8,'bold'))
questionCButton.grid(row = 3, column = 3, sticky = S)
questionDButton = Button(self, text='Submit',font=('MS', 8,'bold'))
questionDButton.grid(row = 3, column = 4, sticky = S)
def main():
ex = Class1(root)
root.geometry("{0}x{1}+0+0".format(root.winfo_screenwidth(),
root.winfo_screenheight()))
root.mainloop()
if __name__ == '__main__':
main()
You are not using self as the parent of label1. Besides, the grid manager starts at row 0:
def question1_UI(self):
# ...
label1 = Label(self, image=gif1)
label1.image = gif1
label1.grid(row = 0, column = 0, columnspan = 2, sticky=NW)
questionAButton = Button(self, text='Submit',font=('MS', 8,'bold'))
questionAButton.grid(row = 1, column = 0, sticky = S)
questionBButton = Button(self, text='Submit',font=('MS', 8,'bold'))
questionBButton.grid(row = 1, column = 1, sticky = S)
questionCButton = Button(self, text='Submit',font=('MS', 8,'bold'))
questionCButton.grid(row = 2, column = 0, sticky = S)
questionDButton = Button(self, text='Submit',font=('MS', 8,'bold'))
questionDButton.grid(row = 2, column = 1, sticky = S)

Categories

Resources