Python Forum
tkinter button acts normal ONLY after clicking off the window
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
tkinter button acts normal ONLY after clicking off the window
#1
I have a mac 10.13.4, and am using python 3.4.4 .
Here is my code:

####################

from tkinter import *

root = Tk()
canvas = Canvas(root, height = 1000, width = 1000)
canvas.pack()

def button_function():
    print("hello")
button = Button(root, text="button", font = ("Helvetica", 25), command=button_function, height = 4, width = 25)
button.place(x = 63, y = 800)
####################

The button command functions perfectly, but the button itself does not show that it is being clicked on when I run the program. However, I accidentally discovered that if I click off the screen an back on, then the button shows when it is being clicked. I'd like to fix my program so that it acts this way all of the time. Any help would be greatly appreciated.
Reply
#2
I'm not getting any error messages.
Reply
#3
You start tkinter with mainloop(), i.e. root.mainloop() in your case. http://effbot.org/tkinterbook/button.htm
Reply
#4
Larz60+: I read the link you gave me. Thank you for editing my post.

wooee: I added root.mainloop() and it doesn't make a difference.
Reply
#5
I took the size arguments out. and use pack across the board,
you can add back)
You can use place, but then will have an issue with resizing.
from tkinter import *

root = Tk()
canvas = Canvas(root)
canvas.pack()


def button_function(event):
    print("hello")


button = Button(root, text="button", font=("Helvetica", 25))
button.pack()
button.bind('<Button-1>', button_function)

root.mainloop()
you forgot mainloop
Reply
#6
Larz60+: I copied your code, and unfortunately it makes no difference on my end.
Reply
#7
There is something wierd with python on the Mac then. This works perfectly on MS windows and Linux
Also python current version is 3.6.5, you're still running python 2.7 (support for this version ends in about 1.5 years).
I don't think that that is an issue with tkinter, but might be/
Onw think I noted is that your import statement uses a lower case 't' on tkinter.
On other systems, tkinter for 2.7 is spelt with an upper case 'T'
Reply
#8
I have been using IDLE (3.4.4) to run python. I tried IDLE 3 (3.6.3) and it works now. They're both "3.something", I'm not sure what the the difference is between "IDLE" and "IDLE 3" is...

The newer version comes with this:
WARNING: The version of Tcl/Tk (8.5.9) in use may be unstable.
Visit http://www.python.org/download/mac/tcltk/ for current information.
which I'll have to address, but I'm just happy this seems to be fixed now.

Thanks everyone.
Reply
#9
idle3 is for python 3.
If you are using python 2.7 you need idle2 (probably just plain idle)
try running from command line:
python programname.py
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Interaction between Matplotlib window, Python prompt and TKinter window NorbertMoussy 3 341 Mar-17-2024, 09:37 AM
Last Post: deanhystad
  [Tkinter] TKinter Remove Button Frame Nu2Python 8 803 Jan-16-2024, 06:44 PM
Last Post: rob101
  tkinter - touchscreen, push the button like click the mouse John64 5 739 Jan-06-2024, 03:45 PM
Last Post: deanhystad
  Tkinter multiple windows in the same window tomro91 1 784 Oct-30-2023, 02:59 PM
Last Post: Larz60+
  Centering and adding a push button to a grid window, TKinter Edward_ 15 4,369 May-25-2023, 07:37 PM
Last Post: deanhystad
  [Tkinter] Open tkinter colorchooser at toplevel (so I can select/focus on either window) tabreturn 4 1,829 Jul-06-2022, 01:03 PM
Last Post: deanhystad
  [Tkinter] Background inactivity timer when tkinter window is not active DBox 4 2,860 Apr-16-2022, 04:04 PM
Last Post: DBox
  [Tkinter] Clicking on the button crashes the TK window ODOshmockenberg 1 2,196 Mar-10-2022, 05:18 PM
Last Post: deanhystad
  why my list changes to a string as I move to another window in tkinter? pymn 4 2,546 Feb-17-2022, 07:02 AM
Last Post: pymn
  Can't get tkinter button to change color based on changes in data dford 4 3,361 Feb-13-2022, 01:57 PM
Last Post: dford

Forum Jump:

User Panel Messages

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