Ben
Ben

Reputation: 1839

reading function from another file into a button in python

I'm making a main code in Python 3.6 called gui_check.py.

The code looks like this:

from tkinter import *
from urlread import givenumbers

top = Tk()
top.geometry("400x400")

B = Button(top, text = "Hello", command = givenumbers())
B.place(x = 50,y = 50)

top.mainloop()

In this code, there is a function called givenumbers() which is a function from another file (called urlread.py) that prints numbers.

The result I wanted to get is a gui with a button that when I click on it, it calls the function givenumber(). However, the result I get is that when I run the code it runs givenumber() (print the numbers) while opening the gui even without me clicking on the button.

Upvotes: 2

Views: 3199

Answers (1)

Leonid
Leonid

Reputation: 738

Remove the parentheses in:

B = Button(top, text = "Hello", command = givenumbers())

So you should have:

B = Button(top, text = "Hello", command = givenumbers)

instead.

Read http://effbot.org/zone/tkinter-callbacks.htm

Upvotes: 2

Related Questions