Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Importing classes?
#3
Object oriented coding in python is a huge mountain for me.I need to study it a lot more to fully understand it.
GUI.py
import tkinter as tk
from tkinter import messagebox

from sys import exit
import os

class GUI:
    def __init__(self,root):
       
####### Gets the requested values of the height and widht.
        windowWidth = root.winfo_reqwidth()
        windowHeight = root.winfo_reqheight()
        
        # Gets both half the screen width/height and window width/height
        positionRight = int(root.winfo_screenwidth()/2 - windowWidth/2)
        positionDown = int(root.winfo_screenheight()/2 - windowHeight/2)
        # Positions the window in the center of the page.
        root.geometry("+{}+{}".format(positionRight, positionDown))
##########----------------------

        
        root.title("Αρχική είσοδος")
        self.maingui=tk.Frame(root)
        self.maingui.pack()
        self.Label1=tk.Label(self.maingui,text="Καλώς ορίσατε στο βοήθημα γραμμικής άλγεβρας")
        self.Label1.pack()
        self.Label2=tk.Label(self.maingui,text="Παρακαλώ,διάλεξε μία από τις επιλογές")
        self.Label2.pack()
                ##
        self.sum_button=tk.Button(self.maingui,text="Πρόσθεση",command=self.sum_py)
        self.sum_button.pack(fill="both",expand=1)
                ##
        self.afairesh_button=tk.Button(self.maingui,text="Αφαίρεση",command=self.afairesi_py)
        self.afairesh_button.pack(fill="both",expand=1)
                ##
        self.pollaplasiasmos_button=tk.Button(self.maingui,text="Πολλαπλασιαμός Πίνακα",command=self.pollaplasiamos_py)
        self.pollaplasiasmos_button.pack(fill="both",expand=1)
                ##
        self.quit_button=tk.Button(self.maingui,text="Έξοδος",command=self.popupmsg)
        self.quit_button.pack(fill="both",expand=1)
        
    def centered(self):
        # Gets the requested values of the height and widht.
        windowWidth = root.winfo_reqwidth()
        windowHeight = root.winfo_reqheight()
        print("Width",windowWidth,"Height",windowHeight)
        # Gets both half the screen width/height and window width/height
        positionRight = int(root.winfo_screenwidth()/2 - windowWidth/2)
        positionDown = int(root.winfo_screenheight()/2 - windowHeight/2)
        # Positions the window in the center of the page.
        root.geometry("+{}+{}".format(positionRight, positionDown))
            
    def kill(self):
        self.centered()
        root.destroy()
        popup.destroy()
        exit(0)
           
    def sum_py(self):
        root.withdraw()
        import prosthesi
        
    def afairesi_py(self):
        root.withdraw()
        import afairesh
        
    def pollaplasiamos_py(self):
        root.withdraw()
        import pollaplasiasmos
        
    def popupmsg(self):
        global popup
        root.withdraw()
        popup = tk.Tk()
        popup.title("Εξοδος")
        popup.iconbitmap('warn_img.ico')
        popup.geometry("200x100")
        warn = tk.Label(popup, text="Έξοδος?",)
        warn.pack()
        Yes= tk.Button(popup,text="Ναί",command =self.kill)
        Yes.pack()
        No= tk.Button(popup,text="Όχι",command = self.switch)
        No.pack()
        popup.resizable(0,0)
        popup.mainloop()
        
    def switch(self):
        popup.destroy()  
        root.deiconify()  
        
        
    
#--------------

if __name__ == '__main__':
    root=tk.Tk()
    guitest=GUI(root)
    root.mainloop()
Module that must be imported

from tkinter import *
import numpy as np
import os
import sys
import GUI
import subprocess 
import importlib


class MyApp:
    def __init__(self,root):
####### Gets the requested values of the height and widht.
        windowWidth = root.winfo_reqwidth()
        windowHeight = root.winfo_reqheight()
        
        # Gets both half the screen width/height and window width/height
        positionRight = int(root.winfo_screenwidth()/2 - windowWidth/2)
        positionDown = int(root.winfo_screenheight()/2 - windowHeight/2)
        # Positions the window in the center of the page.
        root.geometry("+{}+{}".format(positionRight, positionDown))
          #### there is a lot more but there are just more functions
The thing that is concerning me is the bottom of the module file
root=Tk()
myapp=MyApp(root)
root.mainloop()
Should i add
if __name__ == "__main__"
??
If i just straight up import them,the code will run before the gui,because i have imported it on the top.
Reply


Messages In This Thread
Importing classes? - by frequency - Dec-30-2018, 02:21 PM
RE: Importing classes? - by ichabod801 - Dec-30-2018, 02:29 PM
RE: Importing classes? - by frequency - Dec-30-2018, 02:42 PM
RE: Importing classes? - by ichabod801 - Dec-30-2018, 03:01 PM
RE: Importing classes? - by frequency - Dec-30-2018, 03:10 PM
RE: Importing classes? - by ichabod801 - Dec-30-2018, 03:22 PM
RE: Importing classes? - by frequency - Dec-30-2018, 07:04 PM
RE: Importing classes? - by frequency - Dec-30-2018, 08:20 PM
RE: Importing classes? - by ichabod801 - Dec-30-2018, 08:27 PM
RE: Importing classes? - by frequency - Dec-31-2018, 03:34 PM
RE: Importing classes? - by frequency - Dec-31-2018, 04:48 PM
RE: Importing classes? - by ichabod801 - Dec-31-2018, 07:21 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Pygame importing classes Padalinkiller1 2 3,832 Nov-15-2017, 02:08 PM
Last Post: Padalinkiller1
  Problems with importing classes in different folder Xeraphim 3 3,441 Nov-08-2017, 03:20 PM
Last Post: Larz60+
  Using classes? Can I just use classes to structure code? muteboy 5 5,179 Nov-01-2017, 04:20 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

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