how to disappear a button after clicking on it in tkinter? - python-3.x

consider the following code:
from tkinter import *
root=Tk()
root.geometry('700x700')
frame=Frame(root)
frame.pack(fill=BOTH,expand=True)
def opencanvas():
canvas=Canvas(frame,bg='red')
canvas.place(x=50,y=50,height=300,width=300)
button2=Button(frame,text='exit',command=canvas.place_forget)
button2.place(x=5,y=10)
button1=Button(frame,text='open',command=opencanvas)
button1.place(x=5,y=10)
mainloop()
this program opens a frame in a tkinter window and a 'open' button in the frame.when pressing 'open',a canvas opens and a button named 'exit' appears in frame at the same place of the open button.exit button hides canvas.
when clicking the 'exit' button,i want to disappear the 'exit' button automatically after performing its operation(ie,hiding canvas). what should i do to work this?
I want to insert some commands inside opencanvas() which disappears the button2 right after it pressed.
nb: the two buttons should place at same place and have same dimensions.

Use button1.place_forget() or .grid_forget() or .pack_forget() depending on which geometry manager you use. If you use these, then the button is hidden. To get the button back use .place() etc...
If you want to, you could use button1.destroy() but this deletes the button permanently, and if you have to make that button again, you have to initialise from start.
Use button.place_forget() for your code to hide the button and then, button1.place(x=5,y=10) to show your button

I found the answer for my question.It can be done by using lambda inside button command as follows.
button2=Button(frame,text='exit',command=lambda:[canvas.place_forget(),button2.place_forget()])
lambda function executes the commands one by one.

Related

How to propagate the parent's click event in Pyside2 as if I have clicked the button directly while hovering over it?

How to propagate the parent's click event in Pyside2 as if I have clicked the button directly while hovering over it?
This is a very peculiar problem, and I haven't been able to find even a question like this.
A button is positioned on the main UI window. When clicked, it hides the parent window, and spawns another window at that position. I need it to work with a single click, for example, I want to press the button, and not release the mouse immediately, but hover over a certain part of the newly spawned window and then release it.
This works perfectly fine if I click the button directly , but the problem is I need to click the button from the main UI window.
I am doing it by sending "mousePressEvent" from the main UI window to the child widget's "mousePressEvent", like this:
def mousePressEvent(self, event): #parent mouse press event
#----
#----
self.FocusedButton.mousePressEvent(event) # child widget
When I activate the button from the mainUI window, the window is immediately hidden and the new window is spawned. However, since I haven't performed mouse release yet, I am stuck into the parent's mouse press event loop. The mouse doesn't invoke paint event and doesn't register mouseMoveEvent until I release the click because the mainUI window is hidden and mouse tracking doesn't work any more.
I have tried setting the flags of the window in every way, (QtCore.Qt.WA_TransparentForMouseEvents,True ) for example, but none of it worked.
The main UI window inherits from "QtWidgets.QWidget" and does not have the clicked method.

Need to remove windows topbar in tkinter

I need to remove the topbar of a window (the title, icon, resize and close window buttons) without root.overrideredirect(True) because this method will toggle other things that I need too. Is there a different way to remove the topbar with tkinter?
The only way to remove it is with overrideredirect.

PyQt: How do you clear focus on startup?

When I start up my PyQt GUI, the focus immediately goes to the text box.
I want there to be no focus on any of the buttons at the start of the program, especially not the text box.
Is there a way to remove the focus entirely or at least move the focus to a button or something?
Thanks
clearFocus() seems to work after a certain amount of delay after the window is visible. I also used setFocus() on the QMainWindow and then the textedit field lost focus.
Create a button with dimensions 0 wide by 0 high.
Set it as the default button and also early in the tab order before the other controlls that except focus; but note that it will be triggered if the user presses ENTER in some edit controls.
Call self.ui.yourbutton.setFocus() if desired for example after restore from minimized

Is there an 'Onload' sort of a signal for QMainWindow?

PyQt noob here.
I need to call a dialog window as soon as the main window is loaded.
I also need the main window to be displayed in the background when this dialog is shown on top of it.
So, I'm looking for a 'onload' sort of a signal for the main window to call the function which in turn calls the dialog. Is there any other way around?
You can override the QWidget.showEvent for your QMainWindow to achieve the same effect. However, you need to keep track of whether it is the first time the window is shown or not since that method will be called every time the window is displayed after being hidden. Then use a modal QDialog so that the main window is shown in the background, but not enabled.

wxPython on KDE 4.2: popping window on top of the screen and hiding back

I want my window to be invisible most of the time, but get to top of
the screen on hotkey and hide again if the user switches to another
application.
In my hotkey handler I use
self.Show()
self.Raise()
self.Iconize(False)
and in my activate message handler
self.Hide()
self.Iconize(True)
but the window pops up on top of the screen, but remains deactivated:
it's title bar colour is a inactive one, and it flashes in the taskbar
as a window requiring my attention and it doesn't get EVT_ACTIVATE. I
tried to add self.SetFocus, but no effect.
And if I use
self.Show()
self.SetFocus()
in hotkey handler and in my activate message handler
self.Hide()
and it works okay if I deactivate my window by clicking to another
window, but if I press Alt-Tab and then invoke my window with a hotkey
it doesn't appear on top of the screen but just flashes in the
taskbar. Also I made an ability to hide it by pressing a button on it,
and if I hide it this way, it also doesn't show correctly afterwards
as in the case with Alt-Tab
you have to use a window manager to activate windows. I'm using wmctrl for that purpose (cannot find the same func. through kwin's dbus for kde).

Resources