Python Forum
[Tkinter] Arguments in function for button command
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Arguments in function for button command
#11
That gives a different error:

Error:
Traceback (most recent call last):
  File "C:\Users\kb\Desktop\Python\GUIcalc2.py", line 12, in <module>
    class FilmCalc(Frame):
  File "C:\Users\kb\Desktop\Python\GUIcalc2.py", line 91, in FilmCalc
    calcButton = Button(root, text="Calculate", command=self.go)
NameError: name 'self' is not defined
Reply
#12
What's really needed is a complete piece of code that I can run to see what is causing the problem.
Up to this point, I'm giving best guess. Code that you have submitted is missing import statements,
class definitions, etc.
Reply
#13
Here is the entire thing. It works up to hitting the calculate button.

from Tkinter import *

root = Tk()
root.geometry("450x350+200+100")

class FilmCalc(Frame):
  
    def __init__(self, parent):
        Frame.__init__(self, parent)    
        self.parent = parent
        self.initUI()
        
    def initUI(self):
        self.parent.title("Film Calculator")
        self.grid

        frame1 = Frame(root)
        frame1.grid

    frame_rate_8=float(17)
    frame_rate_s8 = float(17)
    frame_rate_16 = float(20)
    frame_rate_16s = float(24)
    fpft8 = float(80)
    fpfts8 = float(72)
    fpft16 = float(40)
    fpsecs8 = fpft8/frame_rate_8
    fpsecss8 = fpfts8/frame_rate_s8
    fpsecs16 = fpft16/frame_rate_16
    fpsecs16s = fpft16/frame_rate_16s
    global spinbox1
    global entry1
    global entry2
    entry1 = IntVar(None)
    entry2 = IntVar(None)
    global time
    
    def go(self, event):
        def time():
            e1= entry1.get()
            e2= entry2.get()
            run_time_min= float(e1)
            run_time_sec= float(e2)
            time= run_time_min * 60 + run_time_sec
            return time
        
        def calc():
            sb1 = spinbox1.get()
            if sb1 == '8mm':
                feet=time/fpsecs8
                print feet
                #tkMessageBox.showinfo("Tada!", feet "feet")
            elif sb1 == 'Super 8':
                feet= time/fpsecss8
                print feet
                #tkMessageBox.showinfo("Tada!", feet "feet")
            elif sb1 == '16mm':
                feet= time/fpsecs16
                print feet
                #tkMessageBox.showinfo("Tada!", feet "feet")
            elif sb1 == '16 Sound':
                feet= time/fpsecs16s
                print feet
                #tkMessageBox.showinfo("Tada!", feet "feet")

    def reset():
            entry1.delete(0, END)
            entry2.delete(0, END)
            spinbox1.selection_clear()

    spinbox1 = Spinbox(root, values= ("8mm", "Super 8", "16mm", "16 Sound"), wrap = TRUE)
    spinbox1.grid(row=0, column=1, padx=105, pady=15)
    
    lbl1 = Label(root, text="Minutes", width=8)
    lbl1.grid(row=2, column=2, pady=15, sticky=W)           
   
    entry1 = Entry(root, justify= RIGHT)
    entry1.grid(row=2, column=1, pady=15)
    
    lbl2 = Label(root, text="Seconds", width=8)
    lbl2.grid(row=3, column=2, sticky=W)        

    entry2 = Entry(root, justify= RIGHT)
    entry2.grid(row=3, column=1)
    
    resetButton = Button(root, text="Reset", command= reset)
    resetButton.grid(row=4, column=1, pady=100)
    
    calcButton = Button(root, text="Calculate", command=go)
    calcButton.grid(row=4, column=2) 
    
    #lbl3 = Label(root, text= entry1.cget())
    #lbl3.grid(row=5, column=1, pady= 100)
  
app = FilmCalc(root)
root.mainloop()
Reply
#14
You are getting these errors because you need to learn how to use classes before using them.
Reply
#15
Thank you for the input. I removed the classes and got it to work.

Thanks Larz60+ for trying to help.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Button 'command' Argument Confusion gw1500se 11 5,685 Nov-11-2021, 08:45 PM
Last Post: menator01
  Creating a function interrupt button tkinter AnotherSam 2 5,415 Oct-07-2021, 02:56 PM
Last Post: AnotherSam
  [Tkinter] Have tkinter button toggle on and off a continuously running function AnotherSam 5 4,918 Oct-01-2021, 05:00 PM
Last Post: Yoriz
  Get name of command button Heyjoe 3 2,237 Dec-10-2020, 04:30 AM
Last Post: deanhystad
  Class function does not create command button Heyjoe 2 2,224 Aug-22-2020, 08:06 PM
Last Post: Heyjoe
  [Tkinter] Command button, then more command buttons Heyjoe 4 2,818 Aug-08-2020, 11:28 AM
Last Post: Yoriz
  [Tkinter] button command tkinter Heyjoe 6 4,990 Jul-30-2020, 07:06 PM
Last Post: deanhystad
  Button Command Heyjoe 4 2,302 Jul-20-2020, 01:45 AM
Last Post: Heyjoe
  Passing arguments into function, tkinter nanok66 3 4,870 Apr-18-2020, 11:53 PM
Last Post: nanok66
  Tkinter:Unable to bind and unbind function with a button shallanq 2 4,965 Mar-28-2020, 02:05 AM
Last Post: joe_momma

Forum Jump:

User Panel Messages

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