Python Forum
Button in one class, methods in another one
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Button in one class, methods in another one
#1
Hi, I spent 2 days trying to solve this little exercice, creating a face with button open or closing his mouth.
I do have 2 questions about this code :

  1. I cannot delete the round face to create a closed mouth, line 40 and line 43, python says : AttributeError: 'Button_Can' object has no attribute 'opened_mouth'. How could I make this work ?
  2. How could I avoid to repeat the circle function on line 17 in the Button_Can Class ?

    Thanks for any help that could enlighten my poor coding skills

    from tkinter import *
    
    class Button_Can(Frame):
    	def __init__(self):
    		Frame.__init__(self)
    		self.larg,self.haut=200,200
    		self.can=Canvas(self.master,width=self.larg,height=self.haut,bg='ivory')
    		self.can.pack()
    		self.b1=Button(self.master,text='Open it !',command=self.openit)
    		self.b2=Button(self.master,text='Close it !',command=self.closeit)
    		self.b1.pack(side=RIGHT)
    		self.b2.pack(side=LEFT)
    	def openit(self):
    		Face.openMouth(self)
    	def closeit(self):
    		Face.closeMouth(self)
    	def circle(self,x, y, r, coul ='black'):
    		"Draw a circle center x,y radius r "
    		self.can.create_oval(x-r, y-r, x+r, y+r, outline=coul)
    
    class Face():
    	def __init__(self,can):
    		self.can=can
    		"Draw a face"
    		can.delete(ALL)
    		cc =[[100, 100, 80, 'red'],[70, 70, 15, 'blue'], 
    		[130, 70, 15, 'blue'],[70, 70, 5, 'black'],
    		[130, 70, 5, 'black'],[44, 115, 20, 'red'], 
    		[156, 115, 20, 'red'],[100, 95, 15, 'purple']] 
    		i =0
    		while i < len(cc):
    			el = cc[i] 
    			self.circle(el[0], el[1], el[2], el[3])
    			i += 1 
    
    	def circle(self,x, y, r, coul ='black'):
    		"Draw a circle center x,y radius r "
    		self.can.create_oval(x-r, y-r, x+r, y+r, outline=coul)
    	def openMouth(self):
    		self.can.delete(self.closed_mouth)
    		self.opened_mouth=self.circle(100, 145, 30, 'purple')
    	def closeMouth(self):
    		self.can.delete(self.opened_mouth)
    		self.closed_mouth=self.can.create_line(80,145,120,145,fill='purple')
    
    A=Button_Can()
    B=Face(A.can)
Reply


Messages In This Thread
Button in one class, methods in another one - by alan9979 - Jul-10-2019, 08:39 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Kivy] Acces atributes from a button defined in a class inside the .kv Tomli 2 2,116 Jun-10-2021, 01:05 AM
Last Post: Tomli
  [Tkinter] Modify Class on Button Click KDog 4 3,988 May-11-2021, 08:43 PM
Last Post: KDog
  Class function does not create command button Heyjoe 2 2,295 Aug-22-2020, 08:06 PM
Last Post: Heyjoe
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 5,044 Jul-21-2019, 06:02 PM
Last Post: FullOfHelp
  Button click doesnt work from my second class/layout in Python imamideb 0 2,372 Feb-13-2018, 12:09 PM
Last Post: imamideb
  Overriding tkinter button methods Lubik_ 3 6,200 Sep-26-2017, 10:24 AM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020