Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Button Command
Hello Python Users

I have a button(mybutton) in a file called startpage. If I click on this button I want it to run a function in a file called om. The function is called openingmenu.

mybutton  = Button(root, text="Click me for opening menu", bg="white", fg="firebrick", \
                 relief = "groove", font = "Helvitica 60", command = from om import openingmenu)
So I tried to set command as command = from om import openingmenu, but I get an invalid syntax error. Can this be done?
import om
mybutton  = Button(root, text="Click me for opening menu", bg="white", fg="firebrick", \
                 relief = "groove", font = "Helvitica 60", command = om.openingmenu)
This is the entire code except for the function, using the changes you suggested.
from tkinter import *
root = Tk()


import om

mybutton  = Button(root, text="Click me for opening menu.", bg="white", fg="firebrick", \
                 relief = "groove", font = "Helvitica 60",command = om.openingmenu)

The code does not create the mybutton. The code runs the function. This produces a menu.

I don't understand why the code runs the function because this is not supposed to happen until after mybutton is clicked.
use a callback

import om
from functools import partial

def callback():
tk.Button(...., command=partial(callback))
Thanks all. I was able to get Deanhystad's solution to work.

Possibly Related Threads…
Thread Author Replies Views Last Post
  Get name of command button Heyjoe 3 365 Dec-10-2020, 04:30 AM
Last Post: deanhystad
  Class function does not create command button Heyjoe 2 479 Aug-22-2020, 08:06 PM
Last Post: Heyjoe
  [Tkinter] Command button, then more command buttons Heyjoe 4 856 Aug-08-2020, 11:28 AM
Last Post: Yoriz
  [Tkinter] button command tkinter Heyjoe 6 1,638 Jul-30-2020, 07:06 PM
Last Post: deanhystad
  tkinter button not accessing the command when clicked jhf2 1 803 Nov-23-2019, 10:17 PM
Last Post: DT2000
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 1,795 Jul-21-2019, 06:02 PM
Last Post: FullOfHelp
  [Tkinter] command button automaticaly fired kferhat 2 887 Jun-16-2019, 08:21 PM
Last Post: kferhat
  [Tkinter] Button command getting TypeError: radsa() missing 3 required positional arguments nonzzo 2 1,244 Apr-22-2019, 12:36 PM
Last Post: Larz60+
  [Tkinter] how to assign a command to a button ChickenWithRice 2 2,525 Dec-08-2018, 09:35 PM
Last Post: ChickenWithRice
  [Tkinter] Tkinter newly created button does not execute command ChipsSlave 7 3,134 Jul-25-2018, 01:01 AM

Forum Jump:

User Panel Messages

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