Python Forum
Tkinter:Unable to bind and unbind function with a button
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tkinter:Unable to bind and unbind function with a button
#1
I'm having trouble binding and unbinding a function(using 2 buttons) to display a keyboard.

I basically want keyboard()to be hidden first, but when the
morn_med_button is clicked, it will appear and it will disappear when confirm_button1 is clicked.

When i run the code, it says:
return self.func(*args)
TypeError:keyboard() takes 0 positional arguments but 1 was given

This is my code below.

from tkinter import *
import RPi.GPIO as GPIO
from tkinter import ttk
from tkinter import font as tkfont
import time


root = Tk()
root.geometry("2000x1900")
root.config(bg="light blue")
root.frame = Frame(root)

morn_med_Text=Text(root,width=20,height=13)
morn_med_label=Label(root,bg="light blue", font=("Arial",15 ))
morn_med_button = Button(root, text="Add Medication", command=add_medication1, font=("Arial", 12))  
morn_med_button.place(relx=0.2, rely = 0.76, anchor = CENTER)
confirm_button1=Button(root, text="Confirm", command=confirm1, font=("Arial", 12)) 


def confirm1():
    global morn_med_label
    morn_med=morn_med_Text.get("1.0",END)
    morn_med_label.place(relx=0.179, rely =0.82, anchor = CENTER)
    morn_med_Text.place_forget()
    confirm_button1.place_forget()
    morn_med_button.place(relx=0.2, rely = 0.76, anchor = CENTER)
    morn_med_label.config(text=morn_med)
    [u]confirm_button1.bind("<Button-1>",keyboard)[/u]


def keyboard():
    morn_med_button.unbind("<Button-1>")
    def select(value):
        if value == "Clear":
            morn_med_Text.delete(1.0,END)
        elif value == "Enter":
             morn_med_Text.insert(INSERT,'\n')
        elif value=="Backspace":
            text=morn_med_Text.get(1.0,END)
            morn_med_Text.insert(text[:-1],END)
        else:
            morn_med_Text.insert(END, value)
    buttons=[
    'q','w','e','r','t','y','u','i','o','p','Backspace','7','8','9','-',
    'a','s','d','f','g','h','j','k','l','(',')','4','5','6','+',
    'z','x','c','v','b','n','m',',','.','Clear','Enter','1','2','3','/',
    ' ',
]

    label1=Label(root,text='')
    label2=Label(root,text='')
    label2.grid(row=7,columnspan=15)
    label3=Label(root,text='')
    label3.grid(row=8,columnspan=15)
    varRow=9
    varColumn=0

    for button in buttons:
        command=lambda x=button: select(x)
        if button!=" ":
            Button(root,text=button,width=7,bg="#000000",fg="#ffffff",
                       activebackground="#ffffff",activeforeground="#000000",relief='raised',padx=9,
                       pady=9,bd=7,command=command,font=("Arial",17 )).grid(row=varRow,column=varColumn)
        if button==" ":
            Button(root,text=button,width=80,bg="#000000",fg="#ffffff",
                       activebackground="#ffffff",activeforeground="#000000",relief='raised',padx=4,
                       pady=4,bd=7,command=command,font=("Arial",20 )).grid(row=13,columnspan=20)
        varColumn+=1
        if varColumn>14 and varRow==9:
            varColumn=0
            varRow+=1
        if varColumn>14 and varRow==10:
            varColumn=0
            varRow+=1

root.mainloop()
Reply
#2
When binding events in tkinter, the fuction you bind needs an event argument. The function you are binding has no arguments.
Reply
#3
declare your functions before you create the buttons unless you create a class
python reads your script from top to bottom:
def add_medication1():
    keyboard()
 
def confirm1():
    global morn_med_label,confirm_button1
    morn_med=morn_med_Text.get("1.0",END)
    morn_med_label.place(relx=0.179, rely =0.82, anchor = CENTER)
    morn_med_Text.place_forget()
    confirm_button1.place_forget()
    morn_med_button.place(relx=0.2, rely = 0.76, anchor = CENTER)
    morn_med_label.config(text=morn_med)
    confirm_button1.bind("<Button-1>",keyboard) 
morn_med_Text=Text(root,width=20,height=13)
morn_med_Text.place(x=300,y=300)
morn_med_label=Label(root,bg="light blue", font=("Arial",15 ))
morn_med_button = Button(root, text="Add Medication", command=add_medication1, font=("Arial", 12))  
morn_med_button.place(relx=0.2, rely = 0.76, anchor = CENTER)
confirm_button1=Button(root, text="Confirm", command=confirm1, font=("Arial", 12))
confirm_button1.place(x=5,y=400)
if you bind a mouse event to a function it requires an argument
def keyboard(event):
    morn_med_button.unbind("<Button-1>")
    def select(value):
        ....# more       
I used event but it could be anything you want- then you can get information from the event like x= event.x to give you the x,y position of the mouse click
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Mouse click without use bind ATARI_LIVE 8 899 Oct-23-2020, 10:41 PM
Last Post: ATARI_LIVE
Star [Tkinter] How to perform math function in different page of Tkinter GUI ravaru 2 102 Oct-23-2020, 05:46 PM
Last Post: deanhystad
  Tkinter - How can I remove the background borders from ttk.Button? TurboC 4 121 Oct-18-2020, 10:58 AM
Last Post: TurboC
  Call local variable of previous function from another function with Python3 & tkinter Hannibal 5 207 Oct-12-2020, 09:16 PM
Last Post: deanhystad
  tkinter | Button color text on Click Maryan 2 142 Oct-09-2020, 08:56 PM
Last Post: Maryan
  tkinter get method is not accepting value when called by function jagasrik 1 161 Sep-16-2020, 05:28 AM
Last Post: Yoriz
  Class function does not create command button Heyjoe 2 236 Aug-22-2020, 08:06 PM
Last Post: Heyjoe
  [Tkinter] Use function from other class (Tkinter) zarize 8 406 Aug-17-2020, 09:47 AM
Last Post: zarize
  [Tkinter] button command tkinter Heyjoe 6 1,260 Jul-30-2020, 07:06 PM
Last Post: deanhystad
  [tkinter] color change for hovering over button teacher 4 567 Jul-04-2020, 06:33 AM
Last Post: teacher

Forum Jump:

User Panel Messages

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