Matt
Matt

Reputation: 1

tkinter button release events with grid

So I am trying to create a python GUI to control four buttons. The GUI buttons are aligned by the .grid(). I want to be able to hold down a button that moves a motor until I release it. I've found examples that work for press and release events but then I cannot make the GUI look the same as the code below

from tkinter import *
from tkinter import ttk

def up(event):
    print("up")
def down(event):
    print("down")   
def left(event):
    print("left")
def right(event):
    print("right")
def stopV(event):
    print("stopV")
def stopH(event):
    print("stopH")

root = Tk()
root.title("Telescope Controller")

mainframe = ttk.Frame(root, padding="3 3 12 12")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
mainframe.columnconfigure(0, weight=1)
mainframe.rowconfigure(0, weight=1)

Bup = ttk.Button(mainframe, text="Up").grid(column=2, row=1, sticky=(W, E))
#Bup.bind("<ButtonPress>",up)
#Bup.bind("<ButtonRelease>",stopV)
Bdwn = ttk.Button(mainframe, text="Down").grid(column=2, row=3, sticky=W)
#Bdwn.bind("<ButtonPress>",down)
#Bdwn.bind("<ButtonRelease>",stopV)
Bl = ttk.Button(mainframe, text="Left").grid(column=1, row=2, sticky=E)
#Bl.bind("<ButtonPress>",left)
#Bl.bind("<ButtonRelease>",stopH)
Br = ttk.Button(mainframe, text="Right").grid(column=3, row=2, sticky=W)
#Br.bind("<ButtonPress>",right)
#Br.bind("<ButtonRelease>",stopH)

for child in mainframe.winfo_children(): child.grid_configure(padx=5, pady=5)

root.mainloop()

When I write the code like this the .bind() does not work and outputs

AttributeError: 'NoneType' object has no attribute 'bind'

When I use examples that work for button press and release events I cannot use the .grid() instead .pack() works. I am not sure how to align the buttons how I want with .pack() so I was wondering if there is a simple way to add button press and release events to the code above.

Thank you

Upvotes: 0

Views: 1591

Answers (2)

Matt
Matt

Reputation: 1

Nevermind wow, it's a simple fix.... for anyone having a similar issue here's the updated working code:

from tkinter import *
from tkinter import ttk

def up(event):
    print("up")
def down(event):    
    print("down")   
def left(event):
    print("left")
def right(event):
    print("right")
def stopV(event):
    print("stopV")
def stopH(event):
    print("stopH")

root = Tk()
root.title("Telescope Controller")

mainframe = ttk.Frame(root, padding="3 3 12 12")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
mainframe.columnconfigure(0, weight=1)
mainframe.rowconfigure(0, weight=1)

Bup = ttk.Button(mainframe, text="Up")
Bup.grid(column=2, row=1, sticky=(W, E))
Bup.bind("<ButtonPress>",up)
Bup.bind("<ButtonRelease>",stopV)
Bdwn = ttk.Button(mainframe, text="Down")
Bdwn.grid(column=2, row=3, sticky=W)
Bdwn.bind("<ButtonPress>",down)
Bdwn.bind("<ButtonRelease>",stopV)
Bl = ttk.Button(mainframe, text="Left")
Bl.grid(column=1, row=2, sticky=E)
Bl.bind("<ButtonPress>",left)
Bl.bind("<ButtonRelease>",stopH)
Br = ttk.Button(mainframe, text="Right")
Br.grid(column=3, row=2, sticky=W)
Br.bind("<ButtonPress>",right)
Br.bind("<ButtonRelease>",stopH)

for child in mainframe.winfo_children(): child.grid_configure(padx=5, pady=5)

root.mainloop()

Upvotes: 0

Novel
Novel

Reputation: 13729

This is a common beginners problem. Although it's used a lot in examples, you cannot put the layout (grid in your case) on the same line as the initialization. All of your buttons need to be like this:

Br = ttk.Button(mainframe, text="Right")
Br.grid(column=3, row=2, sticky=W)

Upvotes: 1

Related Questions