Python Tkinter Attribute Error When Using Two Classes - python

Hi there in my code I'm am getting an the error AttributeError: 'GUI' object has no attribute 'calculate_button' And I don't know why its not working when I am using two classes. Also I don't want to make the class inherit each other nor make it an object.
Here is my code:
from tkinter import *
from tkinter import ttk
class Logic:
def __init__(self,a,b,c,d):
self.a = a
self.b = b
self.c = c
self.d = d
as_meter = dict(mm=0.001, cm=0.01, inch=0.0254,ft=0.3048, yd=0.9144, m=1.0,km=1000.0, mi=1609.344,)
def calculate(self, *args):
try:
v = float(c)
except ValueError:
self.d.set('Invalid Input')
return
m = v * sign * self.as_meter [self.a]
r = m/self.as_meter[self.b]
answer = (self.d.set("{:.5g}".format(r)))
return answer
class GUI:
def __init__(self,root):
self.notebook = ttk.Notebook(root)
self.notebook.pack()
self.length_frame = ttk.Frame(self.notebook)
self.notebook.add(self.length_frame, text = 'Length')
#Combobox
self.measurements = StringVar()
self.Combobox_Length_Left = ttk.Combobox(self.length_frame, textvariable = self.measurements, values = ('mm', 'cm', 'inch', 'ft', 'yd', 'm', 'km', 'mi'), width = 10,state=['readonly'])
self.Combobox_Length_Left.current(5)
self.Combobox_Length_Left.grid(row = 2, column = 0, padx = 5, pady = 5, sticky = E)
self.measurements1 = StringVar()
self.Combobox_Length_Right = ttk.Combobox(self.length_frame, textvariable = self.measurements1, value = ('mm', 'cm', 'inch', 'ft', 'yd', 'm', 'km', 'mi'), width = 10,state = ['readonly'])
self.Combobox_Length_Right.current(5)
self.Combobox_Length_Right.grid(row = 2, column = 2, padx = 5, pady = 5, sticky = E)
#Labels
self.Conversion = ttk.Label(self.length_frame, text = 'Convertion:').grid(row = 1, column = 0, padx = 5, pady = 5, sticky = W)
self.Label_Blank = ttk.Label(self.length_frame, text = '').grid(row = 1, column = 1, padx = 5, pady = 5, sticky = E)
self.Label_To2 = ttk.Label(self.length_frame, text = 'to').grid(row = 2, column = 1, padx = 5, pady = 5, sticky = E)
self.Label_To = ttk.Label(self.length_frame, text = 'to').grid(row = 3, column = 1, padx = 5, pady = 5, sticky = E)
#Entry Boxes
self.Text_Length_Left = StringVar()
self.Entry_Length_Left = ttk.Entry(self.length_frame, textvariable = self.Text_Length_Left,width = 13)
self.Entry_Length_Left.grid(row = 3, column = 0, padx = 5, pady = 5)
self.Text_Length_Right = StringVar()
self.Entry_Length_Right = ttk.Entry(self.length_frame, textvariable = self.Text_Length_Right,width = 13, state='readonly')
self.Entry_Length_Right.grid(row = 3, column = 2, padx = 5, pady = 5)
def calculate_button(self):
c_l = self.measurements.get()
c_r = self.measurements1.get()
e_l = self.Text_Length_Left.get()
e_r = self.Text_Length_Right.get()
c = Logic(c_l,c_r,e_l,e_r)
self.Text_Length_Right.set('')
self.Text_Length_Right.set(c.calulate())
#Button
self.Convert_Button = ttk.Button(self.length_frame, text = 'Convert',command = calculate_button, width = 10).grid(row = 1, column = 2, sticky = S)
def main():
root = Tk()
app = GUI(root)
root.title('Metric Calculator')
root.resizable(False,False)
root.mainloop()
main()
Thanks for any help!

There are few issues with your code:
As #Tadhg McDonald-Jensen commented you, calculate_button() is wrongly intended and does not belong to GUI() class
As #MotKohn answered, you must remove () from calculate_button() in Convert_Button button.
Logic() is not defined neither in GUI() nor in Converversion() classes.

unindent calculate_button 1 level. It should not be a function in the init function rather a function of the class GUI. Remove () as mentioned in comment.
There is still a bug,
NameError: name 'Logic' is not defined

Related

OOP in Tkinter - Create Frame with Listbox

Hey I would like to create a frame which contains a Listbox and some buttons. What I would like to do is to somehow pass as an argument a name of this listbox. This is my current code in which I set the name of this listbox to master.thisListbox but I would like to pass it somehow as argument. Is it possible or should I separately crate a listbox in my App and the pass it as an argument to a frame? I'm going to create a lot of such listboxes in my App hence the name of listbox shouldn't be the same. I'm pretty new in OOP.
class myApp(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.dateFrame = DateFrame(self)
self.dateFrame.grid(row = 0, column = 0)
print(self.day.get())
self.myFrame = ListboxFrame(self, "Label!", [1,2,3])
self.myFrame.grid(row = 1, column = 0)
x = self.thisListbox.get(0, END)
print(x)
class DateFrame(ttk.Frame):
def __init__(self, master):
ttk.Frame.__init__(self, master)
# Day
ttk.Label(self, text = "Day", width = 4).grid(row = 0, column = 0, padx = 3, pady = 3)
master.day = IntVar(master, value = 31)
self.dayCB = ttk.Combobox(self, values = [x for x in range(1, 32)], textvariable = master.day, width = 4)
self.dayCB.grid(row = 1, column = 0, padx = 3, pady = 3)
# Month
ttk.Label(self, text = "Month", width = 6).grid(row = 0, column = 1, padx = 3, pady = 3)
self.month = IntVar(master, value = 12)
self.monthCB = ttk.Combobox(self, values = [x for x in range(1, 13)], textvariable = self.month, width = 4)
self.monthCB.grid(row = 1, column = 1, padx = 3, pady = 3)
# Year
ttk.Label(self, text = "Year", width = 4).grid(row = 0, column = 2, padx = 3, pady = 3)
self.year = IntVar(master, value = 2021)
self.yearCB = ttk.Spinbox(self, from_ = 2020, to = 2100, textvariable = self.year, width = 6)
self.yearCB.grid(row = 1, column = 2, padx = 3, pady = 3)
class ListboxFrame(ttk.Frame):
def __init__(self, master, labelText, values, selectmode = "extended", height = 6, width = 30):
ttk.Frame.__init__(self, master)
# Listbox
ttk.Label(self, text = labelText).grid(row = 0, column = 0, columnspan = 3)
master.thisListbox = tk.Listbox(self, selectmode = selectmode, height = height, width = width)
master.thisListbox.grid(row = 1, column = 0, columnspan = 3, padx = 2, pady = 2)
# Entry
self.entry = ttk.Entry(self)
self.entry.grid(row = 2, column = 0, padx = 2, pady = 2)
# Buttons
self.addButton = ttk.Button(self, text = "Add", width = 4, command = self.Add)
self.addButton.grid(row = 2, column = 1, padx = 2, pady = 2)
self.deleteButton = ttk.Button(self, text = "Delete", width = 6, command = self.Delete)
self.deleteButton.grid(row = 2, column = 2, padx = 2, pady = 2)
for v in values:
master.thisListbox.insert(END, v)
def Add(self):
if self.entry.get() == "": return
master.thisListbox.insert(END, self.entry.get())
self.entry.delete(0, END)
def Delete(self):
for index in reversed(master.thisListbox.curselection()):
master.thisListbox.delete(index)
# listbox.config(height = listbox.size())

Grid_forget() doesn't seem to forget widgets

I have a problem regarding tkinter's grid_forget() method. I have 2 pages in a notebook and I want to show certain widgets on the second page based off the user's selected options in the first page, I have a multiple choice menu and the grid_forget() method seems to work well until I select 2 or more options from the menu. I tried creating the widgets when an option is selected and place them based on the choice, no luck there, also tried creating them with the rest of the widgets and when the user selected an option I would simply just use grid to place them on the screen, also no luck there. I created a demo below, sorry for potential mistakes.
import tkinter as tk
from tkinter import ttk
SMALL_FONT = ("calibri", 16)
class App(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
self.notebook = ttk.Notebook(self, height = "900", width = "1600")
self.notebook.grid(row = 0, column = 0)
self.frame_pasul3 = tk.Frame(self.notebook)
self.frame_pasul1 = tk.Frame(self.notebook)
self.notebook.add(self.frame_pasul1, text = "First page")
self.notebook.add(self.frame_pasul3, text = "Second page")
self.first_page()
def first_page(self):
options = ["Urmarire mobiliara",
"Urmarire imobiliara",
"Predarea silita bunuri imobile",
"Predarea silita bunuri mobile",
"Obligatia de a face",
"Executare minori"]
menubutton_modalitate_exec = tk.Menubutton(self.frame_pasul1, text="Alegeti o modalitate de executare",
indicatoron=True, borderwidth=1, fg = "#000000",relief="raised")
menu_modalitate_exec = tk.Menu(menubutton_modalitate_exec, tearoff=False)
menubutton_modalitate_exec.configure(menu=menu_modalitate_exec)
menubutton_modalitate_exec.grid(row = 4, column = 1)
self.modalitate = {}
for choice in options:
self.modalitate[choice] = tk.StringVar()
menu_modalitate_exec.add_checkbutton(label=choice, variable=self.modalitate[choice],
onvalue=1, offvalue=0,
command=self.printValues)
self.second_page()
def second_page(self):
self.frame3_titlu_exec = ttk.Frame(self.frame_pasul3)
self.frame3_titlu_exec.grid()
self.frame3_text = ttk.Frame(self.frame_pasul3)
self.frame3_text.grid()
self.frame3_creante = tk.Frame(self.frame_pasul3)
self.frame3_creante.grid()
self.frame3_reprezentand_obligatia = tk.Frame(self.frame_pasul3)
self.frame3_judecatorie = ttk.Frame(self.frame_pasul3)
self.frame3_judecatorie.grid()
self.frame3_texte = tk.Frame(self.frame_pasul3)
self.frame3_texte.grid()
ttk.Label(self.frame3_titlu_exec, font = SMALL_FONT, text = "Titlu Executoriu").grid(row = 0, column = 0, columnspan = 4 ,pady = 10)
ttk.Button(self.frame3_titlu_exec, text = "Contract de credit.").grid(row = 1, column = 0, padx = 10, ipadx = 15, ipady = 5)
ttk.Button(self.frame3_titlu_exec, text = "Sentinta civila.").grid(row = 1, column = 1, padx = 10, ipadx = 15, ipady = 5)
ttk.Button(self.frame3_titlu_exec, text = "Contract notarial.").grid(row = 1, column = 2,padx = 10, ipadx = 15, ipady = 5)
ttk.Button(self.frame3_titlu_exec, text = "Act de adjudecare.").grid(row = 1, column = 3, padx = 10, ipadx = 15, ipady = 5)
self.entry = tk.Text(self.frame3_text, height = 2, wrap = "word", font = ("Helvetica", 10))
self.entry.grid(row = 0, column = 1, padx = 10, pady = 15)
ttk.Button(self.frame3_text, text = "Incarca titlu").grid(row = 0, column = 2, padx = 10, pady = 15)
ttk.Label(self.frame3_creante, font = SMALL_FONT, text = "Creante").grid(row = 0, column = 0)
self.btn_adauga_creante = ttk.Button(self.frame3_creante, text = "Adauga")
self.btn_adauga_creante.grid(row = 0, column = 3)
self.reprezentand_fapt_label = ttk.Label(self.frame3_judecatorie, font = SMALL_FONT, text = "Ce reprezinta fapta.")
self.reprezentand_fapt_label.grid(row = 2, column = 1)
self.reprezentand_creante = tk.Text(self.frame3_judecatorie, height = 3, width = 70)
self.reprezentand_creante.grid(row = 3 , column = 1, pady = 15)
ttk.Label(self.frame3_texte, font = SMALL_FONT, text = "Judecatorie").grid(row = 4, column = 1)
options_jud = ["optiunea 2.",
"test 3",
"test 4",
"test 5"]
self.judecatorie = ttk.Combobox(self.frame3_texte, values = options_jud)
self.judecatorie.set("Selecteaza o judecatorie.")
self.judecatorie.grid(row = 5, column = 1)
ttk.Button(self.frame3_texte, text = "Pasul 2. Parti dosar.").grid(row = 6, column = 0, padx = 15, ipadx = 15, ipady = 5)
ttk.Button(self.frame3_texte, text = "Pasul 4. Cheltuieli de executare").grid(row = 6, column = 3, ipadx = 15, ipady = 5)
def printValues(self):
for name, var in self.modalitate.items():
if var.get() == "1" and (name == "Predarea silita bunuri imobile" or name == "Predarea silita bunuri mobile" or name == "Obligatia de a face" or name == "Executare minori"):
self.reprezentand_creante_label = tk.Label(self.frame3_judecatorie, font = SMALL_FONT, text = "Ce reprezinta creanta.")
self.reprezentand_creante = tk.Text(self.frame3_judecatorie, wrap = "word", height = 3, width = 70)
self.ok_modalitate_exec = 1
self.reprezentand_creante_label.grid(row = 0, column = 1, padx = 15, pady = 15)
self.reprezentand_creante.grid(row = 1, column = 1, padx = 15, pady = 15)
print("Avem 1 la cele 4", name, var.get())
break
elif var.get() == "0" and (name == "Predarea silita bunuri imobile" or name == "Predarea silita bunuri mobile" or name == "Obligatia de a face" or name == "Executare minori"):
print("Avem 0 la cele 4", name, var.get())
self.ok_modalitate_exec = 0
self.reprezentand_creante_label.grid_forget()
self.reprezentand_creante.grid_forget()
break
if __name__ == "__main__":
main_window = tk.Tk()
app = App(main_window)
app.grid()
main_window.mainloop()

Tkinter Get Value and then Destroy

I'm trying to create an app that has 2 windows. The first window (A) is only called the first time the app is ran, and then opens window (B). For all future events, only window (B) is called. This is the following code:
# Script Counter/Setup
def read_counter():
if path.exists("counter.json"):
return loads(open("counter.json", "r").read()) + 1
else:
info = tk.Tk()
info.title("Setup")
info.geometry("350x120")
info.grid_columnconfigure((0, 2), weight = 1)
count = tk.Label(info, text = "Previous Post Number")
string = tk.StringVar()
count_input = tk.Entry(info, textvariable = string)
val = string.get()
def destroy_get():
val = int(count_input.get())
info.quit()
return val
count_button = tk.Button(info, text = "Done!", command = destroy_get)
tk.Label(info, text = "Setup", font='Helvetica 18 bold').grid(row = 0, column = 1, padx = 5, pady = 5)
count.grid(row = 1, column = 0, padx = 5, pady = 5)
count_input.grid(row = 1, column = 2, padx = 5, pady = 5)
count_button.grid(row = 2, column = 1, padx = 5, pady = 5)
info.mainloop()
# info.destroy()
return destroy_get()
def write_counter():
with open("counter.json", "w") as f:
f.write(dumps(counter))
counter = read_counter()
atexit.register(write_counter)
folders = ["to_post/", "not_posted/", "posted"]
for folder in folders:
if not path.exists(folder):
os.mkdir(folder)
print(os.getcwd())
# GUI
window = tk.Tk()
window.title("Confessions Bot")
window.geometry("600x350")
window.grid_columnconfigure((0,2), weight = 1)
label_tell_account = tk.Label(window, text = "Tellonym Account")
label_tell_password = tk.Label(window, text = "Tellonym Password")
label_ig_account = tk.Label(window, text = "Instagram Account")
label_ig_password = tk.Label(window, text = "Instagram Password")
tell_account = tk.Entry(window)
tell_password = tk.Entry(window)
ig_account = tk.Entry(window)
ig_password = tk.Entry(window)
image = ImageTk.PhotoImage(Image.open("logo.png"))
tk.Label(window, image = image).grid(row = 0, column = 1, padx = 10, pady = 10)
label_tell_account.grid(row = 1, column = 0)
tell_account.grid(row = 1, column = 2, padx = 10, pady = 10)
label_tell_password.grid(row = 2, column = 0, padx = 10, pady = 10)
tell_password.grid(row = 2, column = 2, padx = 10, pady = 10)
label_ig_account.grid(row = 3, column = 0, padx = 10, pady = 10)
ig_account.grid(row = 3, column = 2, padx = 10, pady = 10)
label_ig_password.grid(row = 4, column = 0, padx = 10, pady = 10)
ig_password.grid(row = 4, column = 2, padx = 10, pady = 10)
# run.grid(row = 5, column = 1, padx = 10, pady = 10)
window.mainloop()
When this is ran I get _tkinter.TclError: image "pyimage1" doesn't exist. I read that this happens since I haven't destroyed my initial window. If I change destroy_get() to have info.destroy() I'm no longer able to get the the entry from window A.
This is my first time using Tkinter and coding in general so any help is welcome.

Making a variable to work in another class without using a static class

I am trying to call a variable or a StringVar from the class called GUI without calling the GUI as the object of the class Conversion. Here is an example of my code:
class GUI:
def __init__(self,root):
c = Conversion(measurements,measurements1,Text_Legnth_Left,Text_Length_Right)
measurements = self.measurements
measurements1 = self.measurements1
Text_Length_Left = self.Text_Legnth_Left
Text_Length_Right = self.Text_Length_Right
self.notebook = ttk.Notebook(root)
self.notebook.pack()
self.length_frame = ttk.Frame(self.notebook)
self.weight_frame = ttk.Frame(self.notebook)
self.temperature_frame = ttk.Frame(self.notebook)
self.notebook.add(self.length_frame, text = 'Length')
#Combobox
self.measurements = StringVar()
self.Combobox_Length_Left = ttk.Combobox(self.length_frame, textvariable = self.measurements, values = ('mm', 'cm', 'inch', 'ft', 'yd', 'm', 'km', 'mi'), width = 10,state=['readonly'])
self.Combobox_Length_Left.current(5)
self.measurements.trace('w', c.update_length())
self.Combobox_Length_Left.grid(row = 2, column = 0, padx = 5, pady = 5, sticky = E)
self.measurements1 = StringVar()
self.Combobox_Length_Right = ttk.Combobox(self.length_frame, textvariable = self.measurements1, value = ('mm', 'cm', 'inch', 'ft', 'yd', 'm', 'km', 'mi'), width = 10,state = ['readonly'])
self.Combobox_Length_Right.current(5)
self.measurements1.trace('w', c.self.update_length())
self.Combobox_Length_Right.grid(row = 2, column = 2, padx = 5, pady = 5, sticky = E)
#Labels
self.Conversion = ttk.Label(self.length_frame, text = 'Conversion:').grid(row = 1, column = 0, padx = 5, pady = 5, sticky = W)
self.Label_Blank = ttk.Label(self.length_frame, text = '').grid(row = 1, column = 1, padx = 5, pady = 5, sticky = E)
self.Label_To2 = ttk.Label(self.length_frame, text = 'to').grid(row = 2, column = 1, padx = 5, pady = 5, sticky = E)
self.Label_To = ttk.Label(self.length_frame, text = 'to').grid(row = 3, column = 1, padx = 5, pady = 5, sticky = E)
#Entry Boxes
self.Text_Length_Left = StringVar()
self.Entry_Length_Left = ttk.Entry(self.length_frame, textvariable = self.Text_Length_Left,width = 13)
self.Text_Length_Left.trace('w', self.update_length)
self.Entry_Length_Left.grid(row = 3, column = 0, padx = 5, pady = 5)
self.Text_Length_Right = StringVar()
self.Entry_Length_Right = ttk.Entry(self.length_frame, textvariable = self.Text_Length_Right,width = 13, state='readonly')
And here is my other class:
class Conversion:
def __init__(self,measurements,measurements1,Text_Legnth_Left,Text_Length_Right):
g = GUI(measurements,measurements1,Text_Legnth_Left,Text_Length_Right)
as_meter = dict(mm=0.001, cm=0.01, inch=0.0254,ft=0.3048, yd=0.9144, m=1.0,km=1000.0, mi=1609.344,)
def update_length(self, *args,g):
try:
v = int(g.self.Text_Length_Left.get())
except ValueError:
g.self.Text_Length_Left.set('')
g.self.Text_Length_Right.set('')
return
m = v * self.as_meter [g.self.measurements.get()]
r = m/self.as_meter[g.self.measurements1.get()]
g.self.Text_Length_Right.set("{:.5g}".format(r))
My attempt of calling the the variables such as measurements was c=Conversion(measurements) I know by doing class GUI(Conversion) will work and make it its object but I can't do it for specific reasons.
Thanks for any help really appreciate it!

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.

Categories

Resources