Python Forum
Button Command - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: Button Command (/thread-28437.html)



Button Command - Heyjoe - Jul-19-2020

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?


RE: Button Command - deanhystad - Jul-19-2020

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()



RE: Button Command - Heyjoe - Jul-19-2020

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.


RE: Button Command - menator01 - Jul-19-2020

use a callback

import om
from functools import partial

def callback():
    om.openingmenu()
tk.Button(...., command=partial(callback))



RE: Button Command - Heyjoe - Jul-20-2020

Thanks all. I was able to get Deanhystad's solution to work.