Python Forum

Full Version: question regarding OOP
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have a piece of code that is structured as a Class object "A". Then I have GUI code which is also structured as a Class object "B". How I possibly could put them together? Does one of them needs to be a subclass or could it work as a two separate classes? Any guidance or ideas would be very helpful! Thanks!
==================== A =======================
class Balance(object):
    def __init__(self, Month, Cash1, Income, Cash2, Investments, Cost):
        self.Month = Month
        self.Cash1 = Cash1
        self.Income = Income
        self.Cash2 = Cash2
        self.Investments = Investments
        self.Cost = self.Cash1 + self.Income - self.Cash2
def main():
     Month1 = Balance('December 2016', 0, 23502, 8204, 67639, 0)  

if __name__=="__main__":
    main()
==================== B =======================
from tkinter import *


class Application(object):

    def __init__(self, master):
        Frame.__init__(self,master)
        self.grid()
        self.create_widgets()

    def create_widgets(self):
        self.submit_button = Button(self, text="Submit",)
        self.submit_button.grid(row=0, column=3)
        self.label_1 = Label(self, text="Insert Month")
        self.label_2 = Label(self, text="Insert Income")
        self.label_3 = Label(self, text="Insert Cash2")
        self.label_4 = Label(self, text="Insert Investments value")
        self.entry_1 = Entry(self)
        self.entry_2 = Entry(self)
        self.entry_3 = Entry(self)
        self.entry_4 = Entry(self)

        self.label_1.grid(row=0, sticky=E)
        self.label_2.grid(row=1, sticky=E)
        self.label_3.grid(row=2, sticky=E)
        self.label_4.grid(row=3, sticky=E)

        self.entry_1.grid(row=0, column=1)
        self.entry_2.grid(row=1, column=1)
        self.entry_3.grid(row=2, column=1)
        self.entry_4.grid(row=3, column=1)
        
        self.text = Text(self, width = 80, heigh = 15, wrap = WORD)
        self.text.grid(row=4, column=0, columnspan =4, sticky =W)

root = Tk()
root.title("Balance 1.1")
root.geometry("700x400")
app = Application(root)
root.mainloop()
Have you considered making "A" and "B" functions within one class?
Example:

from tkinter import *

Class Something(object): 

    Def Balance(self):
        #"A" goes here

    Def Application(self):
        #"B" goes here
 
Doug - Please use code tags

I have fixed your post this time
Typically you code using the Model-View-Controller pattern. In that case, your Balance class is your Model, and your Application would be both the View and the Controller, but ideally you have a window which is the View, and some logic elsewhere which is the Controller.