Python Forum

Full Version: Make a fully functional program as a library
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello!,
I am creating a GUI that will unite all the 3 programs together.3 differend buttons for 3 differend .py files. How can i make each one of them able to work as soon as i tab a button on my GUI? The whole story is will i import it as a library or ??????? Test code here:

from tkinter import *
import numpy as np
import os
import sys
class MyApp:
    def __init__(self,root):
        root.title("Πρόσθεση πινάκων")
        self.root=Frame(root)
        self.root.pack()
        self.start()
        self.entries1=[]
        self.L1=[]
        self.L2=[]
        self.Q=[]
        self.F=[]
        
    def start(self):
        self.w=Label(self.root,text="Πολλαπλασιασμός πινάκων με αριθμό παραδείγματα",font="Arial 12")
        self.w.grid(row=1,column=1)
        self.w1=Label(self.root,text="Διάλεξε τις διαστάσεις του πίνακα",font="Arial 12")
        self.w1.grid(row=2,column=1)
        self.s1=Label(self.root,text="X:",font="Arial 10")
        self.entry1=Entry(self.root,font="Arial 12",bg="#d9d9d9",width=5)
        self.entry1.grid(row=3,column=1)
        self.s2=Label(self.root,text="Y:",font="Arial 10")
        self.s2.grid(row=4,column=1)
        self.entry2=Entry(self.root,font="Arial 12",bg="#d9d9d9",width=5)
        self.entry2.grid(row=4,column=1)
        self.w2=Label(self.root,text="Διάλεξε αριθμό",font="Arial 12")
        self.w2.grid(row=5,column=1)
        self.entry3=Entry(self.root,font="Arial 12",bg="#d9d9d9",width=5)
        self.entry3.grid(row=6,column=1)
        self.w3=Button(self.root,text="Σχηματισμός πίνακα ",font="Arial 12",bg="#d9d9d9",command=self.multi)
        self.w3.grid(row=10,column=1)
        
    def creation(self):
        num="0123456789"
        entry_value1=self.entry1.get()
        entry_value2=self.entry2.get()
        entry_value3=self.entry3.get()
        
        if entry_value1 not in num or entry_value2 not in num or entry_value3 not in num:
            root.withdraw()
            alertwindow=Toplevel()
            alertwindow.title("Προσοχή")
            alertlabel=Label(alertwindow,text="Παρακαλω,βαλε μονο ακαιρεους αριθμους!")
            alertlabel.pack()
            alertbutton=Button(alertwindow,text="Ξαναπροσπάθησε",command=self.restart)
            alertbutton.pack()
            
        if entry_value1=="" or entry_value2=="" or entry_value3=="":
            root.withdraw()
            empty_alertwindow=Toplevel()
            empty_alertwindow.title("Προσοχή")
            empty_alertlabel=Label(empty_alertwindow,text="Παρακαλω,συμπλήρωσε ολα τα πεδία")
            empty_alertlabel.pack()
            empty_alertbutton=Button(empty_alertwindow,text="Ξαναπροσπάθησε",command=self.restart)
            empty_alertbutton.pack()
        
        
        entry_value1=int(entry_value1)
        entry_value2=int(entry_value2)
        d=entry_value1*entry_value2
        s=Label(self.root,text=" Πίνακας:",font="Arial 15")
        s.grid(row=12,column=1)
        for i in range(entry_value1):
            for w in range(entry_value2):
                entry=Entry(self.root,font="Arial 12",bg="#d9d9d9",width=5)
                entry.grid(row=12+i,column=2+w)
                self.entries1.append(entry)
        global q
        q=Button(self.root,text="Αποτέλεσμα",font="Arial 14",state='active',command=self.result_lock)
        q.grid(row=14+entry_value1,column=1)
       
    def result(self):
        f=0
        entry_value1=self.entry1.get()
        entry_value2=self.entry2.get()
        entry_value3=self.entry3.get()
        entry_value1=int(entry_value1)
        entry_value2=int(entry_value2)
        entry_value3=int(entry_value3)
        
        d=entry_value1*entry_value2
        for x in self.entries1:
            res1=x.get()
            res1=int(res1)
            self.L1.append(res1)
        for i in range(entry_value1):
            self.L2.append(list(self.L1[f:f+entry_value2]))
            f+=entry_value2
        for x in self.L2:
            for i in range(entry_value2):
                self.Q.append(x[i])
        r=Label(self.root,text="Εδώ είναι αναλυτικά οι πράξεις:",font="Arial 12")
        r.grid(row=18+entry_value1,column=1)
        for c in range(d):
            self.F.append(self.Q[c]*entry_value3)
        c=0   
        for t in range(entry_value1):
            for i in range(entry_value2):
                f=Label(self.root,text="{}*{}={}".format(self.Q[c],entry_value3,self.F[c]),font="Arial 12")
                f.grid(row=18+entry_value1+t,column=2+i)
                c+=1
                    
        e=Label(self.root,text="Ο πίνακας που προκύπτει είναι:",font="Arial 12")
        e.grid(row=20+entry_value1+d,column=1)
        c=0
        for t in range(entry_value1):
            for i in range(entry_value2):
                e=Label(self.root,text="{}".format(self.F[c]),font="Arial 12")
                e.grid(row=20+entry_value1+d+t,column=2+i)
                c+=1
        g=Button(self.root,text="Ξαναπροσπάθησε",font="Arial 12",command=self.refresh)
        g.grid(row=28+2*entry_value1+d,column=1)
    def refresh(self):
        self.root.destroy()
        myapp=MyApp(root)
        
    def result_lock(self):
        self.result()
        q['state']=DISABLED
        
    def restart(self): 
        os.execl(sys.executable, sys.executable, *sys.argv)
        
    def multi(self):
        self.creation()
        self.w3['state']=DISABLED
        

#-----------------------------------------------
root=Tk()
myapp=MyApp(root)
root.mainloop()


Thanks
You did not post the other two programs. How are we to know how they are constructed and what each button is supposed to do? So come up with a simple set up programs that illustrate what you want to do, instead of pasting a program with everything including the kitchen sink that you expect us to trudge through. The general method is to use a separate class that will do what you want (we dont't know what you want or what the button is supposed to do), in a separate program file if you like, and create as many instances of it as you want buttons, but again, there is too little information for anything further.
Sure,take a look at the other programs.
from tkinter import *
import numpy as np
import os
import sys

class MyApp:
    def __init__(self,root):
        root.title("Αφαίρεση πινάκων")
        self.root=Frame(root)
        self.root.pack()
        self.start()
        self.entries1=[]
        self.L1=[]
        self.entries2=[]
        self.L2=[]
        self.L3=[]
        self.L4=[]
        self.Q=[]
        self.Z=[]
        self.F=[]       
    def start(self):
        self.label1=Label(self.root,text="Αφαίρεση πινάκων παραδείγματα",font="Arial 12")
        self.label1.grid(row=1,column=1)
        self.label2=Label(self.root,text="Διάλεξε τις διαστάσεις του πρώτου πίνακα",font="Arial 12")
        self.label2.grid(row=2,column=1)
        self.s1=Label(self.root,text="X:",font="Arial 10")
        self.entry1=Entry(self.root,font="Arial 12",bg="#d9d9d9",width=5)
        self.entry1.grid(row=3,column=1)
        self.s2=Label(self.root,text="Y:",font="Arial 10")
        self.s2.grid(row=4,column=1)
        self.entry2=Entry(self.root,font="Arial 12",bg="#d9d9d9",width=5)
        self.entry2.grid(row=4,column=1)
        self.w2=Label(self.root,text="Διάλεξε τις διαστάσεις του δεύτερου πίνακα",font="Arial 12")
        self.w2.grid(row=5,column=1)
        self.s3=Label(self.root,text="X:",font="Arial 10")
        self.s3.grid(row=6,column=1)
        self.entry3=Entry(self.root,font="Arial 12",bg="#d9d9d9",width=5)
        self.entry3.grid(row=6,column=1)
        self.s4=Label(self.root,text="Y:",font="Arial 10")
        self.s4.grid(row=7,column=1)
        self.entry4=Entry(self.root,font="Arial 12",bg="#d9d9d9",width=5)
        self.entry4.grid(row=7,column=1)
        self.w3=Button(self.root,text="Σχηματισμός πινάκων ",font="Arial 12",bg="#d9d9d9",command=self.multi)
        self.w3.grid(row=8,column=1)		
    def check(self):
        num="0123456789"
        self.entry1_value=self.entry1.get()
        self.entry2_value=self.entry2.get()
        self.entry3_value=self.entry3.get()
        self.entry4_value=self.entry4.get()
        if self.entry1_value not in num or self.entry2_value not in num or self.entry3_value not in num or self.entry4_value not in num:
            root.withdraw()
            alertwindow=Toplevel()
            alertwindow.title("Προσοχή")
            alertlabel=Label(alertwindow,text="Παρακαλώ,βάλε μόνο ακαίρεους αριθμούς!")
            alertlabel.pack()
            alertbutton=Button(alertwindow,text="Ξαναπροσπάθησε",command=self.restart)
            alertbutton.pack()
						
        elif self.entry1_value=="" or self.entry2_value=="" or self.entry3_value=="" or self.entry4_value=="":
            root.withdraw()
            empty_alertwindow=Toplevel()
            empty_alertwindow.title("Προσοχή")
            empty_alertlabel=Label(empty_alertwindow,text="Παρακαλω,συμπλήρωσε ολα τα πεδία")
            empty_alertlabel.pack()
            empty_alertbutton=Button(empty_alertwindow,text="Ξαναπροσπάθησε",command=self.restart)
            empty_alertbutton.pack()
        if self.entry1_value!=self.entry3_value or self.entry2_value!=self.entry4_value:
            self.answer=Label(self.root,text="Δεν μπορείς να προσθέσεις πίνακες διαφορετικών διαστάσεων",font="Arial 15")
            self.answer.grid(row=9,column=1)
            self.clear=Button(self.root,text="Ξαναπροσπάθησε",font="Arial 12",command=self.refresh)
            self.clear.grid(row=11,column=1)     
        else:
            self.creation()
    def creation(self):
        global q
        entry1_value=self.entry1.get()
        entry2_value=self.entry2.get()
        entry3_value=self.entry3.get()
        entry4_value=self.entry4.get()
        entry1_value=int(entry1_value)
        entry2_value=int(entry2_value)
        d=entry1_value*entry2_value
        s=Label(self.root,text="1ος πίνακας:",font="Arial 15")
        s.grid(row=12,column=1)
        for i in range(entry1_value):
            for w in range(entry2_value):
                entry=Entry(self.root,font="Arial 12",bg="#d9d9d9",width=5)
                entry.grid(row=12+i,column=2+w)
                self.entries1.append(entry)
        s=Label(self.root,text="2ος πίνακας:",font="Arial 15")
        s.grid(row=15+entry1_value,column=1)
        for i in range(entry1_value):
            for w in range(entry2_value):
                entry=Entry(self.root,font="Arial 12",bg="#d9d9d9",width=5)
                entry.grid(row=15+entry1_value+i,column=2+w)
                self.entries2.append(entry)
                
        q=Button(self.root,text="Αποτέλεσμα",font="Arial 14",state='active',command=self.result_lock)
        q.grid(row=16+2*entry1_value,column=1)      
    def result(self):
        f=0
        entry1_value=self.entry1.get()
        entry2_value=self.entry2.get()
        entry3_value=self.entry3.get()
        entry4_value=self.entry4.get()
        entry1_value=int(entry1_value)
        entry2_value=int(entry2_value)
        d=entry1_value*entry2_value
        for x in self.entries1:
            res1=x.get()
            res1=int(res1)
            self.L1.append(res1)
        for t in self.entries2:
            res2=t.get()
            res2=int(res2)
            self.L2.append(res2)
        for i in range(entry1_value):
            self.L3.append(list(self.L1[f:f+entry2_value]))
            self.L4.append(list(self.L2[f:f+entry2_value]))
            f+=entry2_value
        for x in self.L3:
            for i in range(entry2_value):
                self.Q.append(x[i])
        for y in self.L4:
            for i in range(entry2_value):
                self.Z.append(y[i])
        r=Label(self.root,text="Εδώ είναι αναλυτικά οι πράξεις:",font="Arial 12")
        r.grid(row=20+2*entry1_value,column=1)
        for c in range(d):
            self.F.append(self.Q[c]-self.Z[c])
        c=0   
        for t in range(entry1_value):
            for i in range(entry2_value):
                f=Label(self.root,text="{}-{}={}".format(self.Q[c],self.Z[c],self.F[c]),font="Arial 12")
                f.grid(row=20+2*entry1_value+t,column=2+i)
                c+=1       
        e=Label(self.root,text="Ο πίνακας που προκύπτει είναι:",font="Arial 12")
        e.grid(row=25+2*entry1_value+d,column=1)
        c=0
        for t in range(entry1_value):
            for i in range(entry2_value):
                e=Label(self.root,text="{}".format(self.F[c]),font="Arial 12")
                e.grid(row=25+2*entry1_value+d+t,column=2+i)
                c+=1
        g=Button(self.root,text="Ξαναπροσπάθησε",font="Arial 12",command=self.refresh)
        g.grid(row=28+2*entry1_value+d+entry1_value,column=1)
    def refresh(self):
        self.root.destroy()
        myapp=MyApp(root)
    def restart(self): 
        os.execl(sys.executable, sys.executable, *sys.argv)
    def multi(self):
        self.check()
        self.w3['state']=DISABLED 
    def result_lock(self):
         self.result()
         q.configure(state="disabled")
         
#-----------------------------------------------------------------------------------------
root=Tk()
myapp=MyApp(root)
root.mainloop()



from tkinter import *
import numpy as np
import os
import sys
class MyApp:
    def __init__(self,root):
        root.title("Πρόσθεση πινάκων")
        self.root=Frame(root)
        self.root.pack()
        self.start()
        self.entries1=[]
        self.L1=[]
        self.L2=[]
        self.Q=[]
        self.F=[]
        
    def start(self):
        self.w=Label(self.root,text="Πολλαπλασιασμός πινάκων με αριθμό παραδείγματα",font="Arial 12")
        self.w.grid(row=1,column=1)
        self.w1=Label(self.root,text="Διάλεξε τις διαστάσεις του πίνακα",font="Arial 12")
        self.w1.grid(row=2,column=1)
        self.s1=Label(self.root,text="X:",font="Arial 10")
        self.entry1=Entry(self.root,font="Arial 12",bg="#d9d9d9",width=5)
        self.entry1.grid(row=3,column=1)
        self.s2=Label(self.root,text="Y:",font="Arial 10")
        self.s2.grid(row=4,column=1)
        self.entry2=Entry(self.root,font="Arial 12",bg="#d9d9d9",width=5)
        self.entry2.grid(row=4,column=1)
        self.w2=Label(self.root,text="Διάλεξε αριθμό",font="Arial 12")
        self.w2.grid(row=5,column=1)
        self.entry3=Entry(self.root,font="Arial 12",bg="#d9d9d9",width=5)
        self.entry3.grid(row=6,column=1)
        self.w3=Button(self.root,text="Σχηματισμός πίνακα ",font="Arial 12",bg="#d9d9d9",command=self.multi)
        self.w3.grid(row=10,column=1)
        
    def creation(self):
        num="0123456789"
        entry_value1=self.entry1.get()
        entry_value2=self.entry2.get()
        entry_value3=self.entry3.get()
        
        if entry_value1 not in num or entry_value2 not in num or entry_value3 not in num:
            root.withdraw()
            alertwindow=Toplevel()
            alertwindow.title("Προσοχή")
            alertlabel=Label(alertwindow,text="Παρακαλω,βαλε μονο ακαιρεους αριθμους!")
            alertlabel.pack()
            alertbutton=Button(alertwindow,text="Ξαναπροσπάθησε",command=self.restart)
            alertbutton.pack()
            
        if entry_value1=="" or entry_value2=="" or entry_value3=="":
            root.withdraw()
            empty_alertwindow=Toplevel()
            empty_alertwindow.title("Προσοχή")
            empty_alertlabel=Label(empty_alertwindow,text="Παρακαλω,συμπλήρωσε ολα τα πεδία")
            empty_alertlabel.pack()
            empty_alertbutton=Button(empty_alertwindow,text="Ξαναπροσπάθησε",command=self.restart)
            empty_alertbutton.pack()
        
        
        entry_value1=int(entry_value1)
        entry_value2=int(entry_value2)
        d=entry_value1*entry_value2
        s=Label(self.root,text=" Πίνακας:",font="Arial 15")
        s.grid(row=12,column=1)
        for i in range(entry_value1):
            for w in range(entry_value2):
                entry=Entry(self.root,font="Arial 12",bg="#d9d9d9",width=5)
                entry.grid(row=12+i,column=2+w)
                self.entries1.append(entry)
        global q
        q=Button(self.root,text="Αποτέλεσμα",font="Arial 14",state='active',command=self.result_lock)
        q.grid(row=14+entry_value1,column=1)
       
    def result(self):
        f=0
        entry_value1=self.entry1.get()
        entry_value2=self.entry2.get()
        entry_value3=self.entry3.get()
        entry_value1=int(entry_value1)
        entry_value2=int(entry_value2)
        entry_value3=int(entry_value3)
        
        d=entry_value1*entry_value2
        for x in self.entries1:
            res1=x.get()
            res1=int(res1)
            self.L1.append(res1)
        for i in range(entry_value1):
            self.L2.append(list(self.L1[f:f+entry_value2]))
            f+=entry_value2
        for x in self.L2:
            for i in range(entry_value2):
                self.Q.append(x[i])
        r=Label(self.root,text="Εδώ είναι αναλυτικά οι πράξεις:",font="Arial 12")
        r.grid(row=18+entry_value1,column=1)
        for c in range(d):
            self.F.append(self.Q[c]*entry_value3)
        c=0   
        for t in range(entry_value1):
            for i in range(entry_value2):
                f=Label(self.root,text="{}*{}={}".format(self.Q[c],entry_value3,self.F[c]),font="Arial 12")
                f.grid(row=18+entry_value1+t,column=2+i)
                c+=1
                    
        e=Label(self.root,text="Ο πίνακας που προκύπτει είναι:",font="Arial 12")
        e.grid(row=20+entry_value1+d,column=1)
        c=0
        for t in range(entry_value1):
            for i in range(entry_value2):
                e=Label(self.root,text="{}".format(self.F[c]),font="Arial 12")
                e.grid(row=20+entry_value1+d+t,column=2+i)
                c+=1
        g=Button(self.root,text="Ξαναπροσπάθησε",font="Arial 12",command=self.refresh)
        g.grid(row=28+2*entry_value1+d,column=1)
    def refresh(self):
        self.root.destroy()
        myapp=MyApp(root)
        
    def result_lock(self):
        self.result()
        q['state']=DISABLED
        
    def restart(self): 
        os.execl(sys.executable, sys.executable, *sys.argv)
        
    def multi(self):
        self.creation()
        self.w3['state']=DISABLED
        

#-----------------------------------------------
root=Tk()
myapp=MyApp(root)
root.mainloop()

Should i try to break the code into classes?
Managed to somehow make it work,by importing them from a def.For example:
def sum_py(self):
        root.withdraw()
        import mymodule
it will work,but if it loads it 2 times,the program disappears.I must have messed up something