-
Notifications
You must be signed in to change notification settings - Fork 0
/
OOP (in Python)
92 lines (83 loc) · 3.32 KB
/
OOP (in Python)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
>>> means command, followed by output
# means comment
''' ''' means multiline comment
---------------------------------------------------------------------------------------------------------------------------------------
'''Basically, an object x of class Name has attributes x.attribute and methods x.method().
The attributes can be changed independently of each other. The changes apply to x independently of other objects of class Name.'''
#example code
#replace value with a number such as 1
#replace f(input, self.attribute) with a function such as input+self.attribute
#replace f(input2, self.attribute) with a function such as input2+self.attribute
class Name: #can also have classes as Inputs, like class Name(Inputs):
attribute=value
def __init__(self, input):
self.attribute2=f(input, self.attribute)
def method(self, input2):
self.attribute2=f(input2, self.attribute)
#example result
#replace input with a number such as 3
#replace input2 with a number such as 5
>>> x=Name(input)
>>> print(x.attribute)
value
>>> print(x.attribute2)
f(input, value)
>>> x.method(input2)
>>> print(x.attribute2)
f(input2,value)
#example module
#save the above code as name.py
>>> from name import Name
>>> x=Name(input)
>>> print(x.attribute)
value
-------------------------------------------------------------------------------------------------------------
'''You can also have subclasses of superclasses (inheritance), and master of self (nested classes).'''
# example pseudocode
#Subclass inherits attributes/methods from Superclass
#a.master = Class
#x = output of Superclass
from tkinter import *
# for some reason the previous line doesn't import messagebox in Python 3.6+
from tkinter import messagebox
class Subclass(Superclass,input=default):
def __init__(self,master):
Superclass.__init__(self)
self.attribute = input
def method(self,x):
return something
class Class(Frame):
def __init__(self,master,inputs=defaults):
Frame.__init__(self,master)
self.grid()
a = Subclass(self, input)
print(a.attribute)
print(a.method())
Tk().title('Title')
b = Class(Tk(), inputs)
Tk().mainloop()
# example result
from tkinter import *
# for some reason the previous line doesn't import messagebox in Python 3.6+
from tkinter import messagebox
class Tile(Button):
def __init__(self,master,x,y): #x,y = coordinates
Button.__init__(self,bg='white',relief='raised',width=22,height=4,font=('Arial',12))
self['text']='click for method(self,x)\n = x\n = output of superclass\n = output of Button'
self.grid(row=y,column=x)
self.bind('<Button-1>',self.method)
self.master=master
def method(self,x):
#x = output of Button (https://github.com/python/cpython/blob/3.8/Lib/tkinter/__init__.py line 1568)
messagebox.showinfo('Title',x,parent=self)
class Board(Frame):
def __init__(self,master,inputs=0):
Frame.__init__(self,master,bg='black')
self.grid()
a = Tile(self,0,1)
print('a.master = ', a.master,' (output of nested class/Board not superclass/Button)') #output of Board not Button
print('board master = ',master,' (output of nested class/Tk() not superclass/Frame)') #output of Tk() not Frame
root=Tk() #every time you type Tk() opens a new window
root.title('Title')
b = Board(root,0)
root.mainloop()