Changing Tkinter Screen when button is clicked? - python

I am aiming to change the a part of the Tkinter screen when a button is clicked. Do I have to destroy the screen then redraw it (to create the illusion that only a part is being changed?) Or can I keep the button there and somehow only change one part (like the graphics.) Thanks!

No, you do not have to destroy the screen and redraw it. You can easily insert widgets into the current window when a button is clicked. There's nothing special about being run from a button click -- the code is the same as your initialization code.

Related

Is it possible to disable the spacebar in Tkinter?

I'm working on a little game with Tkinter, but I'm facing a issue and I dont know how to fix it
The issue is that sometime when I press a button in the game (like the right arrow to move right for example), the function corresponding to the button is working well, but when I press spacebar just after, it calls the function again which is something I dont want.
So I'm wondering if there is any way to completly disable the space bar for tkinter ?
I've already tried 2 things:
Bind the spacebar to another function which is doing nothing, but when I press the spacebar it is still repeating the last button pressed
Unbind the space bar at the start of the code like this:
import tkinter as tk
game = tk.Tk()
game.unbind('<space>')
If you want, here is the full code of the game:
https://github.com/Nirs123/World_Of_Boats
Appreciate every feedback :)
Here is an answer of what I said in my comment.
You can use game.unbind_class() to unbind a specific event for a specific type of widget. To unbind the spacebar from a button, you can use game.unbind_class("Button", "<Key-space>"). You can call it right after you create game, and all the buttons in the whole window will no longer be able to be pressed with the spacebar.

Make edges of button disappear in tkinter

I have a customized button, on which I loaded an icon like this:
but as you can see there's a "box" around the button. The icon itself doesn't have background, so I guess the box is added by tkinter to indicate that this is a button. Is there a way to make those four lines disappear?
Thanks in advance.

How to expand window/frame when button or checkbox is clicked in tkinter?

I am using Tkinter for my python script. At current stage, gui looks ugly because of too many options/widgets in one screen. Not all of them are needed all the time and it can be divided into four parts. Hence my idea is to have them expand the frame (one frame for each part) with its options/widgets in the current window.
Is it possible in tkinter to expand the window/frame when a button or a checkbox is clicked?
ps - I have tried opening each part in new window but this makes gui unnecessarily complicated.
Thanks!
So, your gonna need to define what happens when the button is clicked-
def buttonClicked():
root.geometry("350x300")
where root is the window and then on the button you will need to write -
btn = tkinter.Button(text = "Button", command=buttonClicked)
btn.pack
Hope that answers your question!

How to hide Gtk widget on clicking outside it?

I have a custom GTK widget (basically an overlay of HBox over a Cairo surface). I wish to hide it when I click outside the widget in the window. Similar to how menus behave.
I tried using grab_focus and wait for the focus-out-event but the widget doesn't grab focus, I think it's not a focusable widget.[1]
[1] https://developer.gnome.org/pygtk/2.24/class-gtkwidget.html#method-gtkwidget--grab-focus
You might have to set the CAN_FOCUS flag, if you want to use the focus_out event.
But if you want to click outside to hide the widget, as is necessary with menus, then you have to connect to events of the area below the widget. You could connect to the button_press event of the window, taking care not to stop event propagation.

'hover over' popup with Tkinter

I have implemented an informational popup in a python app using a Tkinter Menu widget. I have a Text widget on a canvas in the root window. I created a Menu widget that has root as its parent. When I detect a mouse hover over the text widget I post the popup menu with menuWidget.post(). When I get a leave event from the text widget my intention was to have the popup disappear by calling menuWidget.unpost(), only the popup menu does not disappear until I click elsewhere outside the text widget.
First, is this a sane method for implementing an informational popup? And can anyone tell me why the popup menu won't disappear?
This is not the right way to do an informational popup. On the Mac and on windows machines menus are native controls. Because of this the unpost command doesn't work because tk cedes control to the system event loop in order to get platform-specific behavior.
What you want is to use instead is a toplevel window with the overrideredirect flag set. This lets you display a borderless window anywhere you want. The upside to this is that you aren't limited to simple text -- you can put anything you want in that toplevel -- another text widget, a canvas, buttons, etc.

Categories