I show Second QMainWindow after click on button in parent QMainWindow
def on_click(self):
window = second_window.MainWindow()
window.show()
Second window not shown (Without any error). But if in Second window I add line:
self.func = functools.partial(self.some_func)
All work correct.
Why it's happens?
I think the problem here is that you are creating the window as a local variable inside the on_click scope. As soon as on_click finishes the window attribute will be destroyed.
Try storing the window in an instance variable:
def on_click(self):
self._window = second_window.MainWindow()
self._window.show()
The functools.partial approach is probably working just because you are already storing it at the instance.
Related
How can I create a binding in tkinter for when someone closes a toplevel window or if it is closed with toplevel1.destroy() or something similar. I am trying to make a small pop-up and when the user closes the main window or toplevel I want to prompt the user to save a file. I have figured out that I can set the actual close button to the function but cannot figure out how to get .destroy() and the closing of the main window to call the function. What should I do to bind the destroy function or window closing function?
Tested code:
import tkinter as tk
class TestWidget(tk.toplevel):
def __init__(self, *args, **kwargs):
tk.Toplevel.__init__(*args, **kwargs)
self.protocol("WM_DELETE_WINDOW", self.close)
def close(self):
print("Closed")
self.destroy()
if __name__ == "__main__":
root = tk.Tk()
TestWidget()
root.mainloop()
So I figured out that if you make a toplevel widget integrated into a class that you can find all the classes with the code below:
for child in root.winfo_children():
print(child)
This returns all the widgets and classes used:
.!testwidget
.!testwidget2
With this I can set up a function in the main window to call the child's close function one by one and this allows me to gain access to all the needed pieces
In this simple pyqt5 app, I have a QPushButton and I define a shortcut for it. I want to change its text every time it is triggered. Problem is that the shortcut works only for the first time. How can I fix it?
from PyQt5.QtWidgets import QPushButton, QMainWindow, QApplication
import sys
class window(QMainWindow):
def __init__(self):
super(window, self).__init__()
self.btn = QPushButton('&Connect', self)
self.btn.setShortcut('Ctrl+C')
self.btn.pressed.connect(self.btn_func)
def btn_func(self):
if self.btn.text() == '&Connect':
self.btn.setText('Dis&connect')
else:
self.btn.setText('&Connect')
def main():
app = QApplication(sys.argv)
win = window()
win.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
As explained in the text property documentation [emphasis mine]:
If the text contains an ampersand character ('&'), a shortcut is automatically created for it. [...] Any previous shortcut will be overwritten or cleared if no shortcut is defined by the text
I know that the above might seem confusing, as it seems that the shortcut is overwritten or cleared only if no shortcut is defined by the text, consider it like this:
Any previous shortcut will be overwritten, or it is cleared if no shortcut is defined by the text
The solution is to always reset the shortcut after setting the new text:
def btn_func(self):
if self.btn.text() == '&Connect':
self.btn.setText('Dis&connect')
else:
self.btn.setText('&Connect')
self.btn.setShortcut('Ctrl+C')
Note that using the button's text for comparison is considered bad practice, for three reasons:
the text of a button could (should) be localized;
you could easily forget to correctly update all the & texts, making the function behave in the wrong way;
some QStyles override existing mnemonics and change them by themselves, which also causes the text to change without any warning;
The most preferred way to achieve what you want would be to use an internal flag for the current state, and also a QAction with its own shortcut.
class Window(QMainWindow):
def __init__(self):
super(Window, self).__init__()
self.btn = QPushButton('&Connect', self)
self.btn.clicked.connect(self.btn_func)
self.connectAction = QAction('Toggle connection', self)
self.connectAction.setShortcut('Ctrl+c')
self.connectAction.triggered.connect(self.btn.animateClick)
self.addAction(self.connectAction)
self.connected = False
def btn_func(self):
self.connected = not self.connected
if self.connected:
self.btn.setText('Dis&connect')
else:
self.btn.setText('&Connect')
Also note that:
you should not use the pressed() signal, as it's standard convention to consider a button clicked when the user presses and releases the mouse button while inside the button area (so that the pressed action could be "undone" by moving the mouse away if the mouse button was pressed by mistake on the button); use the clicked() signal instead;
I changed the class name using a capitalized Window, as classes should always use capitalized names and lower cased names should only be used for functions and variables;
I used the animateClick slot to show that the button was clicked (as a visual feedback is always preferable), but you can directly connect to the function: self.connectAction.triggered.connect(self.btn_func);
I'd like to create a dynamic menu which enumerates all QDockWidget from my QMainWindow and allows to show/hide the QDockWidgets, so far I got this code:
class PluginActionsViewDocks():
def __init__(self, main_window):
self.main_window = main_window
mapper = QSignalMapper(self.main_window)
self.actions = []
for dock in main_window.findChildren(QtWidgets.QDockWidget):
action = create_action(
main_window, dock.windowTitle(),
slot=mapper.map,
tooltip='Show {0} dock'.format(dock.windowTitle())
)
mapper.setMapping(action, dock)
self.actions.append(action)
mapper.mapped.connect(self.toggle_dock_widget)
help_menu = main_window.menuBar().addMenu('&View')
setattr(help_menu, "no_toolbar_policy", True)
add_actions(help_menu, tuple(self.actions))
def toggle_dock_widget(self, dock_widget):
print("toggle_dock_widget")
The menu is populated with all QDockWidget windowTitles but when i press each of them the slot toggle_dock_widget is not called. create_action is a helper which creates the QAction and connect the triggered signal to slot.
The thing is, I don't really understand quite well how QSignalMapper works but my intuition tells me it's the right choice for this particular problem.
What could I be missing here?
There's aleady a built-in dock-widget menu. Just right-click any dock title-bar, or any tool-bar or menu-bar. See: QMainWindow::createPopupMenu.
PS:
The reason why your QSignalMapper code doesn't work is probably because you are connecting to the wrong overload of the mapped signal. Try this instead:
mapper.mapped[QtWidgets.QWidget].connect(self.toggle_dock_widget)
I am just starting to get into python and I am utterly confused as to how object creation works. I am trying to create user interface with GTK. Here is an example of the problem I am having:
from gi.repository import Gtk
def button_clicked(self, button):
self.button_label = button.get_label()
if self.button_label == "Login":
window.quit()
window2.start()
class LoginWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="AMOK Cloud")
self.connect("delete-event", Gtk.main_quit)
self.set_position(position = Gtk.WindowPosition.CENTER)
# Button
self.loginbutton = Gtk.Button(label="Login")
self.loginbutton.connect("clicked", button_clicked(self, self.loginbutton))
self.add(self.loginbutton)
self.show_all()
Gtk.main()
def quit(self):
self.close()
Gtk.main_quit()
class MainWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="AMOK Cloud")
self.connect("delete-event", Gtk.main_quit)
self.set_position(position=Gtk.WindowPosition.CENTER)
def start(self):
self.show_all()
Gtk.main()
window = LoginWindow()
window2 = MainWindow()
Error comes up as NameError: name 'window' is not defined
even though I did define window. I don't understand. If someone can explain it would mean the world to me. Thanks in advance.
EDIT:
Thanks guys this works fine now, took Gtk.main() out of both classes and added button_clicked method inside LoginWindow() class and now it works like a charm. I assumed I needed Gtk.main() for every window I open.
It is because you are starting the main loop (Gtk.main()) inside of LoginWindow.__init__(). That means that the window = LoginWindow() line doesn't finish executing until after the login window is closed. You should take Gtk.main() outside of the __init__ method, and move it to the last line in the file. As mentioned by PM 2Ring in the comments, you don't need to call Gtk.main() twice. Take it out completely in MainWindow.start() because the one now added to the last line in the file takes care of that. Also mentioned by PM, connect() calls a function when an event happens. When you give it button_clicked(...), that function is called and you are actually telling connect() to call whatever is returned, None. If you want special arguments, use a lambda, but you aren't even changing anything (those are the default arguments anyway), so you can simply do this:
self.connect("clicked", button_clicked)
I would also suggest that instead of making button_clicked a separate function, make it a static method of the class. You do that by placing it inside the class, but with #staticmethod just above the def line. That way, it makes sense for it to take the self argument, but you don't need two parameters to account for the same window.
I have a button connected to a function called OpenSupplyWidget() which is supposed to start a QWidget class I have in another file (the file is SupplyWidget.py and is already imported).
def OpenSupplyWidget(self):
sw = SupplyWidget()
sw.show()
The function only opens the window for a split second. Using sys.exit(app.exec_()) only returns an error saying the QApplication event loop is already running.
What method do I use to get what I want (opening a widget)?
Thanks!
Try having the SupplyWidget outside of the OpenSupplyWidget-function. The sw probably gets destroyed because the function terminates.
...
def __init__(self):
self.sw = SupplyWidget()
def OpenSupplyWidget(self):
self.sw.show()
..