Python Forum

Full Version: Button Command
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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)
Mybutton.pack()
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)
Mybutton.pack()
This is the entire code except for the function, using the changes you suggested.
from tkinter import *
root = Tk()

root.geometry("700x700")

import om

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

mybutton.pack()
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():
    om.openingmenu()
tk.Button(...., command=partial(callback))
Thanks all. I was able to get Deanhystad's solution to work.