Python Forum
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
question regarding OOP
#1
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()
Reply
#2
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
 
Reply
#3
Doug - Please use code tags

I have fixed your post this time
Reply
#4
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.
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply


Forum Jump:

User Panel Messages

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