Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
GUI and PDf
#1
I just started using Python about a week ago and I ran into a road block. I've been trying to make a GUI where a user can select a page on the main menu that will direct them to some type of survey and they can fill in the text boxes. When you press submit I want it to take the variables from the text box and format it on a PDF. I've been playing around with one variable for testing. I have the PDF structured on another program. I'm only working with the ROC and sub page with the variable typeweight and typeweight_info.

 
import tkinter as tk
from tkinter import ttk
from fpdf import FPDF

LARGE_FONT= ("Verdana", 14)

class MassCal(tk.Tk):    
    def __init__(self, *args, **kwargs):        
        tk.Tk.__init__(self, *args, **kwargs)
        
      
        self.shared_data = {
            'typeweight' : tk.StringVar()
        }
        
        # use gimp to convert to ICO
        tk.Tk.iconbitmap(self, "NPSL.ico")
        tk.Tk.wm_title(self, "NAVY PRIMARY STANDARDS LAB")

        
        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand = True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)
        
        self.frames = {}
        
        for F in (StartPage, beginWeighing, ROC, sub):
        
            frame = F(container, self)

            self.frames[F] = frame

            frame.grid(row=0, column =0, sticky="nsew")

        self.show_frame(StartPage)
        
    def show_frame(self, cont):
    
        frame = self.frames[cont]
        frame.tkraise()
        
    #def get_page(self, page_class):
     #   return self.frames[page_class]
        
#================End up base setup=================


#=====================================PAGES SETUP==============================

                    #================Main page Class==================
    
class StartPage(tk.Frame):
    
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="""NAVY PRIMARY STANDARDS LAB""", font=LARGE_FONT)
        
        
        label.pack(pady=50, padx=50)
        
        button1 = ttk.Button(self, text="Begin Weighing", width = 40,  command = lambda: controller.show_frame(beginWeighing))
        button1.place(x = 15, y = 200)
        
        
        button2 = ttk.Button(self, text="Print Report (ROC)", width = "40",
                            command=lambda: controller.show_frame(ROC))
        button2.place(x = 15, y = 400)
        
#        button3 = ttk.Button(self, text="Print Label", width = "40", height = "10",
#                            command=lambda: controller.show_frame(Label))
#        button3.place(x = 15, y = 600)
        
        
        
                     #================Page One Class ==============
        
class beginWeighing(tk.Frame):
    
    def __init__(self, parent, controller):
        self.controller=controller
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="Begin Weighing", font=LARGE_FONT)
        label.pack(pady=50, padx=50)   
        
        
        
        button1 = ttk.Button(self, text="Back", 
                            command=lambda: controller.show_frame(StartPage))
        button1.place(x = 15, y =530)
        
    
#        button2 = ttk.Button(self, text="Next", 
#                            command=lambda: controller.show_frame(PageTwo))
#        button2.place(x = 100, y = 730)


    
class sub(tk.Frame):

    def __init__(self, parent, controller):
        self.controller=controller
        
        
        
        typeweight_info = self.controller.shared_data['typeweight'].get()
        
        
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="PDF Confirmation page", font=LARGE_FONT)
        label.pack(pady=50, padx=50)   
       
        #page1 = self.controller.get_page(ROC)
        #page1.typeweight.
        #typeweight_info = typeweight
        #typeweight_info = typeweight.get()
        
        pdf = FPDF("P", "in", )                     
        pdf.set_margins(.3, .8, .4)
        pdf.alias_nb_pages()
        pdf.add_page()
        pdf.set_font("times", size = 12)
        pdf.cell(0, 1, txt = typeweight_info , ln=1, align= "C")
        pdf.set_font("times", size = 12)
        pdf.output("simple_demo.pdf", 'F')

        button1 = ttk.Button(self, text="Back to main menu", 
                            command=lambda: controller.show_frame(StartPage))
        button1.place(x = 15, y =530)  
    

                    #=================Page Two Class ==============

class ROC(tk.Frame):
    
    
    
    def __init__(self, parent, controller):
        self.controller=controller
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="Report (ROC)", font=LARGE_FONT)
        label.pack(pady=50, padx=50)   
        
         
        #====This creates labels for the text boxes=====
        typeweight_text = tk.Label(self, text = " Type of weight set *" ,)
        manufacturer_text = tk.Label(self, text = "Manufacturer * " ,)
        model_text = tk.Label(self, text = "Model *")
        serial_text = tk.Label(self, text = "Serial *")
        submitted1_text = tk.Label(self, text = "Submitted by 1 *") 
        submitted2_text = tk.Label(self, text = "Submitted by 2 *")
        standardweight_text = tk.Label(self, text = "Standard Weight *")
        tolerance_text = tk.Label(self, text = "Tolerance *")
        ambienttemp_text = tk.Label(self, text = "Ambient Temperature *") 
        relativehumid_text = tk.Label(self, text = "Relative Humidity *") 
        caldate_text = tk.Label(self, text = "Calibration Date *") 
        duedate_text = tk.Label(self, text = "Due Date *")
        rocno_text = tk.Label(self, text = "ROC # *")  

        #====This positions the labels based on coordinates===
        typeweight_text.place(x = 15, y = 60)
        manufacturer_text.place(x = 15, y = 110)
        model_text.place(x=15 , y=160)
        serial_text.place(x = 15, y = 210)
        submitted1_text.place(x = 15, y = 260)
        submitted2_text.place(x = 15, y = 310)
        standardweight_text.place(x = 15, y = 360)
        tolerance_text.place(x = 15, y = 410)
        ambienttemp_text.place(x = 15, y = 460)
        relativehumid_text.place(x = 15, y = 510)
        caldate_text.place(x = 15, y = 560)
        duedate_text.place(x = 15, y = 610)
        rocno_text.place(x = 15, y = 660)

        #======Here we initialize variables that will be used to take in user input=====
        #typeweight = tk.StringVar()
        manufacturer = tk.StringVar()
        model = tk.StringVar()
        serial = tk.StringVar()
        submitted1 = tk.StringVar()
        submitted2 = tk.StringVar()
        standardweight = tk.StringVar()
        tolerance = tk.StringVar()
        ambienttemp = tk.StringVar()
        relativehumid = tk.StringVar()
        caldate = tk.StringVar()
        duedate = tk.StringVar()
        rocno = tk.StringVar()

        #=====This creates textbox for users to input info. Input will be stored into variables=====
        typeweight_entry = tk.Entry(self, textvariable = self.controller.shared_data['typeweight'], width = "30")
        
        
        manufacturer_entry = tk.Entry(self, textvariable = manufacturer, width =  "30")
        model_entry = tk.Entry(self, textvariable = model, width = "30")
        serial_entry = tk.Entry(self, textvariable = serial, width = "30")
        submitted1_entry = tk.Entry(self, textvariable = submitted1, width = "30")
        submitted2_entry = tk.Entry(self, textvariable = submitted2, width = "30")
        standardweight_entry = tk.Entry(self, textvariable = standardweight, width = "30")
        tolerance_entry = tk.Entry(self, textvariable = tolerance, width = "30")
        ambienttemp_entry = tk.Entry(self, textvariable = ambienttemp, width = "30")
        relativehumid_entry = tk.Entry(self, textvariable = relativehumid, width = "30")
        caldate_entry = tk.Entry(self, textvariable = caldate, width = "30")
        duedate_entry = tk.Entry(self, textvariable = duedate, width = "30")
        rocno_entry = tk.Entry(self, textvariable = rocno, width = "30")

        #====This positions the textbox====
        typeweight_entry.place(x = 15, y = 80)
        manufacturer_entry.place(x = 15, y = 130)
        model_entry.place(x = 15, y = 180)
        serial_entry.place(x = 15, y = 230)
        submitted1_entry.place(x = 15, y = 280)
        submitted2_entry.place(x = 15, y = 330)
        standardweight_entry.place(x = 15, y = 380)
        tolerance_entry.place(x = 15, y = 430)
        ambienttemp_entry.place(x = 15, y = 480)
        relativehumid_entry.place(x = 15, y = 530)
        caldate_entry.place(x = 15, y = 580)
        duedate_entry.place(x = 15, y = 630)
        rocno_entry.place(x = 15, y = 680)
              
        
        
        
        button1 = ttk.Button(self, text="Back", 
                            command=lambda: controller.show_frame(StartPage))
        button1.place(x = 550, y = 730)
        
        
        button2 = ttk.Button(self, text="Submit", 
                            command=lambda: controller.show_frame(sub))
        button2.place(x = 700, y =730)

        
    
  
            
app = MassCal()
#ani = animation.FuncAnimation(f, animate, interval=1000)
app.mainloop()
Reply
#2
So far there is no question in your post...
Do you get any error or just not what you want? If former - post full traceback in error tags. Otherwise - explain what you expect vs what you get as result.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
I think kevin wants to get the entries and save to pdf.(my interpretation),,
A couple of observations: Keep your code simple. it's easier to read and
modify later on. Start on a small scale.
You could do this with one class- this will make it easier
to pass attributes and objects. The use of the prefix self makes objects
global. Make a list of fields and use a for loop to create all you labels and entries.
Lastly I don't have the fpdf module but I hope this points you in the right direction.
Great you made a class let's deal with what you have.
you made this entry widget:
def __init__(self, parent, controller):
    #omitting the rest....
    manufacturer = tk.StringVar()
    manufacturer_entry = tk.Entry(self, textvariable = manufacturer, width =  "30")
    button2 = ttk.Button(self, text="Submit", 
                            command=lambda: controller.show_frame(sub))
    button2.place(x = 700, y =730)
modified to:
def __init__(self, parent, controller):
    #omitting the rest....
    self.manufacturer = tk.StringVar()
    manufacturer_entry = tk.Entry(self, textvariable = self.manufacturer, width =  "30")
    button2 = ttk.Button(self, text="Submit", 
                            command= self.get_info)
    button2.place(x = 700, y =730)
def get_info(self):
     manu= self.manufacturer.get()
     print(manu)
know this also can be done without the textvariable I don't see the point in using
in this application. here's a way to pull the string from the entry widget:
def __init__(self, parent, controller):
    #omitting the rest....
    
    self.manufacturer_entry = tk.Entry(self, width =  "30")
    button2 = ttk.Button(self, text="Submit", 
                            command= self.get_info)
    button2.place(x = 700, y =730)
def get_info(self):
     manu= self.manufacturer_entry.get()
     print(manu)
Reply


Forum Jump:

User Panel Messages

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