0

I'm brand new to python (started today). I'm looking to automate something I do a lot of, so far I have 40% of what I need from googling and trying things out that I have found.

I'm trying to produce a counter, when I click the button, I want the counter to increase - I have this bit working...

from tkinter import *

root = Tk()

#Definitions of the fruit, links in with buttons with e1/2/3 
def Appleadd_1(event):
    value = int(e1.get())
    value += 1
    e1.delete(0, 'end')
    e1.insert(0, value)


def Pearadd_1():
    value = int(e2.get())
    value += 1
    e2.delete(0, 'end')
    e2.insert(0, value)


def Grapeadd_1():
    value = int(e3.get())
    value += 1
    e3.delete(0, 'end')
    e3.insert(0, value)


#text boxes for counts
e1 = tk.Entry(root)
e1.insert(0, 0) 
e1.pack()

e2 = tk.Entry(root)
e2.insert(0, 0) 
e2.pack()

e3 = tk.Entry(root)
e3.insert(0, 0) 
e3.pack()


#buttons
bt1 = tk.Button(root, text="Apples", command=Appleadd_1)
bt1.bind("<q>" , )
bt1.pack() 

bt2 = tk.Button(root, text="Pears", command=Pearadd_1)
bt2.pack()
bt2.bind("1", bt2)

bt3 = tk.Button(root, text="Grapes", command=Grapeadd_1)
bt3.pack()


root.mainloop() 

Although it looks ugly, it works and I have just found how to place things instead of using pack()

One thing I can't get to work, is binding a keyboard key to the buttons I have created. Is it possible?

Any help would be greatly appreciated!

Thank you

  • Read [tkinter-events-and-bindings](http://effbot.org/tkinterbook/tkinter-events-and-bindings.htm) – stovfl Dec 15 '19 at 20:01
  • Does this answer your question? [python tkinter how to bind key to a button](https://stackoverflow.com/questions/13326940/python-tkinter-how-to-bind-key-to-a-button) – stovfl Dec 15 '19 at 20:02
  • Thank you, I have read both of these but I am still none the wiser. I will look through it again and hope it starts to make sense! – Lairy_Fish Dec 15 '19 at 20:06
  • 1
    I have now put the `.bind` after the function and it works. Thank you for your help, another look did help! – Lairy_Fish Dec 15 '19 at 20:37

1 Answers1

0

First off, you'll gain a lot by rewriting your code to use OOP. Tkinter like many other toolkits work best when using inheritance and classes to group widgets together.

As for your actual question, yes you can bind functions to keystrokes in tkinter, and it's relatively easy.

import tkinter as tk

def on_button_click(self, event=None): # command= takes a function with no arguments while .bind takes a function with one argument
    print("Clicked the button!")

root = tk.Tk()
button = tk.Button(root, text="Click me!", command=on_button_click)
root.bind("<Control-a>", on_button_click)

Note that you can bind more than keypresses to functions. You can also bind mouse events like scrolling, clicking, or dragging the mouse, various keybind combinations like Shift+Tab, or Ctrl+F, and other events like the <Configure> event which is triggered when the window changes size, or the <Enter> and <Leave> event which are fired when you hover over the bound widget.

You must be wary though, because by default, a new binding will replace the existing binding (unless you pass in '+' as the bind method's third argument), and will trigger the callback for the widget that is currently focused (when applicable). For general purpose bindings, you should bind them to the root widget if possible.

Dogeek
  • 182
  • 9
  • Thank you, this was relly useful. After a lot more reading and rial and error, I have realised that I won't be able to use Tkinter, as I can only press the button when the window is in focus! Idealy I wanted something running in the background, so I will start again. Thank you for the input though – Lairy_Fish Dec 16 '19 at 14:00