-1

The goal of this project is to have a user be able to enter a m and b value into a tkinter window and have a graph show up when a button is pressed. There is multiple windows. There is a click to start button, and a menu to select the type of graph. Currently I am just focusing on getting the linear graph working. When the linear window is reached it prompts the user to enter a m and b value (through entries).

Window Here

When the graph button is pressed I have coded for it to print the m and b value out. As of right now I cannot get it to print out in the console. If anyone could help up let me know thanks!

Code:

from tkinter import *

root = Tk()
root.geometry("500x500")

class testInputs():
    def __init__(self,m,b):
        #self.displaygframe = Frame(root)
        #self.displayglabe = Label(self.displaygframe,
                                  #text="Test for now",
                                  #font= "ComicSansMS 14")
        print(m)
        print(b)
        
        

class selectGraph(): #2
    def __init__(self):
        self.gselectframe = Frame(root)
        self.gselectlabel = Label(self.gselectframe,
                                  text = "Select Graph Type",
                                  font= "ComicSansMS 14")
        
        
        self.Var = IntVar()
        
        #define buttons
        self.linearButton = Radiobutton(self.gselectframe,
                                        text="Linear",
                                        variable = self.Var,
                                        value=1)
        
        self.quadraticButton = Radiobutton(self.gselectframe,text="Quadratic",
                                           variable = self.Var, 
                                           value = 2)
             
        
        self.confirmButton = Button(self.gselectframe, text = "Continue", command = self.transferAnswer,
                                    fg = "red", font = "ComicSansMS 14",
                                    bd = 2, bg = "light green")
        
        
        self.sgBack = Button(self.gselectframe, text = "Back", command = self.backButton,
                             fg = "red", font = "ComicSansMS 5",
                             bd = 2, bg = "light green")      
        
        
        #pack 
        self.gselectlabel.pack()
        self.linearButton.pack(anchor=W)
        self.quadraticButton.pack(anchor=W)   
        self.confirmButton.pack()
        self.gselectframe.pack()
        

        mainloop()
    def transferAnswer(self):
        answer= self.Var.get()
        print(str(answer))
        self.gselectframe.destroy()
        getEquation(answer)
        
    def backButton(self):
        self.gselectframe.destroy()
        introWindow()
        
class getEquation(): #3
    def __init__(self, answer):
        if answer == 1:
            self.linearInput()
            
        if answer == 2:
            self.quadraticInput()
            
        else:
            selectGraph()
            
    def linearInput(self):
        self.linearFrame = Frame(root)
        
        #define widgets
        self.linearLabel = Label(self.linearFrame,
                                  text = "Enter Linear Equation",
                                  font= "ComicSansMS 14")
        
        
        self.linearBack = Button(self.linearFrame, text = "Back", command = self.backButtonLinear,
                             fg = "red", font = "ComicSansMS 5",
                             bd = 2, bg = "light green")
        
        
        formatLabel = Label(self.linearFrame,
                                  text="Format:",
                                  font="ComicSansMS 14")
        
        equationLabel = Label(self.linearFrame,
                              text="y=mx+b",
                              font="ComicSansMS 14",
                              fg="midnight blue")
        
        #name = StringVar()
        #name2 = StringVar()
        
        self.mLabel = Label(self.linearFrame,
                            text= "m=",
                            font= "ComicSansMS 14")
        
        self.mInput = Entry(self.linearFrame,
                            width =2, font="ComicSansMS 14")
        
        self.bLabel = Label(self.linearFrame,
                            text = "b=",
                            font = "ComicSansMs 14")
        self.bInput = Entry(self.linearFrame,
                            width=2,
                            font="ComicSansMS 14")
        #get info from widget
        m = self.mInput.get()
        b = self.mInput.get()        
        
        self.graphButton = Button(self.linearFrame, text = "Graph", command = self.nextFromLinear(m,b),  #error from here
                                fg = "red", font = "ComicSansMS 14",
                                bd = 2, bg = "light green")

    
        
        #place widgets on screen
        #self.linearFrame.pack()
        
        self.linearLabel.grid(row=0,column=0)
        formatLabel.grid(row=1,column=0)
        equationLabel.grid(row=2, column=0)
        
        
        self.bInput.grid(row=4, column=1)
        self.bLabel.grid(row=4, column=0)
        
        
        self.mInput.grid(row=3, column=1)
        self.mLabel.grid(row=3,column= 0)
        
        self.graphButton.grid(row=5, column=1) #error from here
        
        self.linearBack.grid(row=5,column=0)
        

        self.linearFrame.pack()
        
        
        mainloop()
        
        
    def quadraticInput(self):
        self.quadraticFrame = Frame(root)
        self.quadraticLabel = Label(self.quadraticFrame,
                                  text = "Enter Quadratic Equation",
                                  font= "ComicSansMS 14")
        self.quadraticLabel.pack()
        
        
        self.quadraticBack = Button(self.quadraticFrame, text = "Back", command = self.backButtonQuadratic,
                             fg = "red", font = "ComicSansMS 5",
                             bd = 2, bg = "light green")                
        
        self.quadraticBack.pack(anchor=SW)
        self.quadraticFrame.pack()
        mainloop()
        
        
    def backButtonLinear(self):
        self.linearFrame.destroy()
        selectGraph()
        
    def backButtonQuadratic(self):
        self.quadraticFrame.destroy()
        selectGraph()
        
    def nextFromLinear(self,m,b):
        
        #self.linearFrame.destroy()
        #testInputs(m,b)
        print(m)
        print(b)
        
        
    #Figuring out how to write singular back button function for all equation Frames:
    #def backButton(self, frame1):
        #frame1.destroy()
        #selectGraph
        
class introWindow(): #1
    def __init__(self):
        self.introframe = Frame(root)
        self.introlabel = Label(self.introframe,
                                text = "Graphing Program",font="ComicSansMS 14")
        self.introbutton = Button(self.introframe, text = "Click to start", command = self.windowRemoval,
                fg = "red", font = "ComicSansMS 14",
                bd = 2, bg = "light green")
        
        self.introlabel.pack()
        self.introbutton.pack()
        self.introframe.pack()
        
        
        mainloop()
    def windowRemoval(self):
        self.introframe.destroy()
        selectGraph()
        
introWindow()

    
    







    
Barmar
  • 741,623
  • 53
  • 500
  • 612
Noah
  • 1
  • There's way too much code here, and you haven't made it clear which part is the problem. Remove the code that's unrelated to the problem. – Barmar May 30 '21 at 22:51
  • The `command` argument needs to be a function reference, not a function call. – Barmar May 30 '21 at 22:53

1 Answers1

0

The command argument to Button() must be a function reference, not a call to the function. You're calling the function when you create the button, not when it's clicked.

Use a lambda to create an anonymous function. And it has to call get() when it's called, you can't call it when creating the button, or you'll get the initial values of the inputs, not what the user has entered into them.

    self.graphButton = Button(self.linearFrame, text = "Graph", 
                            command = lambda: self.nextFromLinear(self.mInput.get(),self.bInput.get()),  #error from here
                            fg = "red", font = "ComicSansMS 14",
                            bd = 2, bg = "light green")
Barmar
  • 741,623
  • 53
  • 500
  • 612