I want my application to toggle fullscreen every time you click on the menu item. So if you click once, it becomes fullscreen, if you click again, it becomes normal again. I tried the following but after I clicked it again, it wouldn't switch.
def Fullscreen(self):
self.fullscreen = False
if not self.fullscreen:
self.root.wm_attributes("-fullscreen", True)
else:
self.root.wm_attributes("-fullscreen", False)
You are missing a key part here. Nothing changes full screen back to True.
Here is a simple example of what you could do to toggle full screen.
import tkinter as tk
class App(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
tk.Button(self, text="Toggle Fullscreen", command=self.fullscreen_toggle).pack()
self.fullscreen = False
def fullscreen_toggle(self):
if self.fullscreen == False:
self.wm_attributes("-fullscreen", True)
self.fullscreen = True
else:
self.wm_attributes("-fullscreen", False)
self.fullscreen = False
app = App()
app.mainloop()
Related
I have two check boxes which are part of a button group. I want to be able to select one, the other, or none. To this end, I have the following:
import sys
from PySide2 import QtCore, QtWidgets
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle('Check Problems')
self.init_widgets()
self.init_layout()
def init_widgets(self):
self.check1_label = QtWidgets.QLabel('Check1')
self.check1 = QtWidgets.QCheckBox()
self.check2_label = QtWidgets.QLabel('Check2')
self.check2 = QtWidgets.QCheckBox()
self.check_buttongroup = QtWidgets.QButtonGroup()
self.check_buttongroup.addButton(self.check1)
self.check_buttongroup.addButton(self.check2)
self.check_buttongroup.buttonPressed.connect(self.check_buttongroup_pressed)
def init_layout(self):
check1_layout = QtWidgets.QHBoxLayout()
check1_layout.addWidget(self.check1_label)
check1_layout.addWidget(self.check1)
check2_layout = QtWidgets.QHBoxLayout()
check2_layout.addWidget(self.check2_label)
check2_layout.addWidget(self.check2)
check_layout = QtWidgets.QVBoxLayout()
check_layout.addLayout(check1_layout)
check_layout.addLayout(check2_layout)
# Central widget
centralWidget = QtWidgets.QWidget()
centralWidget.setLayout(check_layout)
self.setCentralWidget(centralWidget)
def check_buttongroup_pressed(self, button):
print('\nIs this check1? ', button == self.check1, flush=True)
print('Status when pressed: ', button.isChecked(), flush=True)
if button.isChecked():
print('Button isChecked: ', button.isChecked(), flush=True)
print('Changing button check state...', flush=True)
button.group().setExclusive(False)
button.setChecked(False)
print('Button isChecked: ', button.isChecked(), flush=True)
button.group().setExclusive(True)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
mainWin = MainWindow()
mainWin.show()
sys.exit(app.exec_())
No boxes are checked initially. Clicking check1 produces:
Is this check1? True
Status when pressed: False
This is expected. The box was unchecked initially. Having clicked it, however, the box is now checked. When I click check1 again, I expect it to uncheck, leaving both boxes unchecked. Now, clicking check1, I see:
Is this check1? True
Status when pressed: True
Button isChecked: True
Changing button check state...
Button isChecked: False
This is as expected. What is unexpected, however, is check1 is still checked!
Clicking a third time produces:
Is this check1? True
Status when pressed: True
Button isChecked: True
Changing button check state...
Button isChecked: False
Why does check1 not uncheck as expected? Is button.isChecked() lying to me or is setChecked(False) not doing its job? Am I mistaken somewhere?
I have tried this with PySide2==5.15.0, PySide2==5.13, PySide6, and PyQt5==5.15 with the same result, so I assume this is how it's supposed to behave and not some Qt implementation error.
The solution is to change the property exclusive to False if when the buttonPressed signal is emitted the button is checked and set that property to true when the buttonClicked is emitted.
def init_widgets(self):
# ...
self.check_buttongroup = QtWidgets.QButtonGroup()
self.check_buttongroup.addButton(self.check1)
self.check_buttongroup.addButton(self.check2)
self.check_buttongroup.buttonPressed.connect(self.handle_pressed)
self.check_buttongroup.buttonClicked.connect(self.handle_clicked)
def handle_pressed(self, button):
button.group().setExclusive(not button.isChecked())
def handle_clicked(self, button):
button.group().setExclusive(True)
I'm trying to setup a window with a notebook and some pages. Within the pages, there will be some entries (see example code).
I'd like to handle the tab key and arrow keys on my own. I don't want the arrow up key to jump to the page title, I don't want the arrow left / right key to cycle through the pages. I don't want the tab key to cycle through the entries.
Connecting to the key-press signal and checking for tab or arrow keys does not really work.
I tried to change the focus chain, but it still focusses on the page title or cycles through the pages with left / rigth arrow.
Any help / idea is highly appreciated.
#!/usr/bin/env python
import pygtk
import gtk
class NotebookExample:
def __init__(self):
window = gtk.Window(gtk.WINDOW_TOPLEVEL)
window.set_size_request(200,200)
notebook = gtk.Notebook()
notebook.set_tab_pos(gtk.POS_TOP)
notebook.show()
hbox1 = gtk.HBox()
notebook.append_page(hbox1, gtk.Label("Page1"))
hbox2 = gtk.HBox()
notebook.append_page(hbox2, gtk.Label("Page2"))
window.add(notebook)
entry1 = gtk.Entry()
entry2 = gtk.Entry()
entry3 = gtk.Entry()
entry1.set_width_chars(5)
entry2.set_width_chars(5)
entry3.set_width_chars(5)
hbox1.pack_start(entry1, False, False, 10)
hbox1.pack_start(entry2, False, False, 10)
hbox1.pack_start(entry3, False, False, 10)
hbox1.set_focus_chain([entry2, entry3])
window.show_all()
def main():
gtk.main()
return 0
if __name__ == "__main__":
NotebookExample()
main()
Found it.
Listening to the key-press-event and returning "True to stop other handlers from being invoked for the event. False to propagate the event further.
window.connect("key-press-event", self.do_key_press)
def do_key_press(self, widget, event):
if gtk.gdk.keyval_name(event.keyval) == 'Tab':
print "tab"
return True
if gtk.gdk.keyval_name(event.keyval) == 'Left' or gtk.gdk.keyval_name(event.keyval) == 'Right':
print "cursor"
return True
return
I'm working on This web sites tutorials for designing my Gui application, but i face problem, in how do i could arrest the progress bar updating in clicking the Button Stop
Actually i see about Gtk.ProgressBar.set_pulse_step() but i still look strange because i am not expert.
Here my code where missed the Stop function.
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, GObject
class ProgressBarWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="ProgressBar Demo")
self.set_border_width(10)
vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6)
self.add(vbox)
self.progressbar = Gtk.ProgressBar()
vbox.pack_start(self.progressbar, True, True, 0)
button = Gtk.Button(label="Start")
button.connect("clicked", self.On_clicking)
vbox.pack_start(button, True, True, 0)
button = Gtk.Button(label="Stop")
button.connect("clicked", self.On_clicking_stop)
vbox.pack_start(button, True, True, 0)
def On_clicking(self, widget):
self.timeout_id = GObject.timeout_add(50, self.on_timeout, None)
self.activity_mode = False
def On_clicking_stop(self, widget):
## I have to stop the Progress Bar on Stop Button click
##
##
##
##
##
return False
def on_timeout(self, user_data):
"""
Update value on the progress bar
"""
if self.activity_mode:
self.progressbar.pulse()
else:
new_value = self.progressbar.get_fraction() + 0.01
if new_value > 1:
new_value = 0
self.progressbar.set_fraction(new_value)
# As this is a timeout function, return True so that it
# continues to get called
return True
win = ProgressBarWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
So i'm looking for the right code of On_clicking_stop() function.
The progressbar is updated using GObject.timeout_add(50, self.on_timeout, None) this is a timeout function that will keep making calls to the specified function till False is returned. Thus in order to make the progressbar stop updating you will have to change on_timeout in such a way that it returns False.
This can for example be done like this:
def On_clicking(self, widget):
self.activity_mode = False
self.updating = True
self.timeout_id = GObject.timeout_add(50, self.on_timeout, None)
def On_clicking_stop(self, widget):
self.updating = False
return True
def on_timeout(self, user_data):
"""
Update value on the progress bar
"""
if self.activity_mode:
self.progressbar.pulse()
else:
new_value = self.progressbar.get_fraction() + 0.01
if new_value > 1:
new_value = 0
self.progressbar.set_fraction(new_value)
# As this is a timeout function, return True so that it
# continues to get called
return self.updating
How could I have a scrollbar inside a gtk.Layout.
For example, in my code I have:
import pygtk
pygtk.require('2.0')
import gtk
class ScrolledWindowExample:
def __init__(self):
self.window = gtk.Dialog()
self.window.connect("destroy", self.destroy)
self.window.set_size_request(300, 300)
self.scrolled_window = gtk.ScrolledWindow()
self.scrolled_window.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
self.window.vbox.pack_start(self.scrolled_window, True, True, 0)
self.layout = gtk.Layout()
self.scrolled_window.add(self.layout)
self.current_pos = 0
self.add_buttom()
self.window.show_all()
def add_buttom(self, widget = None):
title = str(self.current_pos)
button = gtk.ToggleButton(title)
button.connect_object("clicked", self.add_buttom, None)
self.layout.put(button, self.current_pos, self.current_pos)
button.show()
self.current_pos += 20
def destroy(self, widget):
gtk.main_quit()
if __name__ == "__main__":
ScrolledWindowExample()
gtk.main()
What I really want is to find some way to make the scroll dynamic. See the example that I put above, when you click any button, another button will be added. But the scrollbar doesn't work.
What can I do to get the scroll bars working?
Does it works if you either use gtk.Window() instead of gtk.Dialog(); or execute self.window.run() after self.window.show_all()?
The difference between Dialog and common Window is that Dialog has its own loop which processes events. As you do not run its run() command, this loop never gets the chance to catch the events, so ScrolledWindow does not receives them, and does not change its size.
I editted the code so that the 3 buttons now show up .Can someone please tell me how to make it so that when I click the button that says Helloworld and application called Helloworld.py will pop-up in another window.Same for the other 2 buttons
!/usr/bin/env python
# menu.py
import pygtk
pygtk.require('2.0')
import gtk
class MenuExample:
def __init__(self):
# create a new window
window = gtk.Window(gtk.WINDOW_TOPLEVEL)
window.set_size_request(200, 100)
window.set_title("GTK Menu Test")
window.connect("delete_event", lambda w,e: gtk.main_quit())
# Init the menu-widget, and remember -- never
# show() the menu widget!!
# This is the menu that holds the menu items, the one that
# will pop up when you click on the "Root Menu" in the app
menu = gtk.Menu()
# Next we make a little loop that makes three menu-entries for
# "test-menu". Notice the call to gtk_menu_append. Here we are
# adding a list of menu items to our menu. Normally, we'd also
# catch the "clicked" signal on each of the menu items and setup a
# callback for it, but it's omitted here to save space.
for i in range(3):
# Copy the names to the buf.
buf = "Test-undermenu - %d" % i
# Create a new menu-item with a name...
menu_items = gtk.MenuItem(buf)
# ...and add it to the menu.
menu.append(menu_items)
# Do something interesting when the menuitem is selected
menu_items.connect("activate", self.menuitem_response, buf)
# Show the widget
menu_items.show()
# This is the root menu, and will be the label
# displayed on the menu bar. There won't be a signal handler attached,
# as it only pops up the rest of the menu when pressed.
root_menu = gtk.MenuItem("Root Menu")
root_menu.show()
# Now we specify that we want our newly created "menu" to be the
# menu for the "root menu"
root_menu.set_submenu(menu)
# A vbox to put a menu and a button in:
vbox = gtk.VBox(False, 0)
window.add(vbox)
vbox.show()
# Create a menu-bar to hold the menus and add it to our main window
menu_bar = gtk.MenuBar()
vbox.pack_start(menu_bar, False, False, 2)
menu_bar.show()
# Create a button to which to attach menu as a popup
button = gtk.Button("HelloWorld")
button.connect_object("event", self.button_press, menu)
vbox.pack_end(button, True, True, 2)
button.show()
button2 = gtk.Button("Scrible")
button2.connect_object("event", self.button_press, menu)
vbox.pack_end(button2, True, True, 2)
button2.show()
button3 = gtk.Button("Final")
button3.connect_object("event", self.button_press, menu)
vbox.pack_end(button3, True, True, 2)
button3.show()
# And finally we append the menu-item to the menu-bar -- this is the
# "root" menu-item I have been raving about =)
menu_bar.append (root_menu)
# always display the window as the last step so it all splashes on
# the screen at once.
window.show()
# Respond to a button-press by posting a menu passed in as widget.
#
# Note that the "widget" argument is the menu being posted, NOT
# the button that was pressed.
def button_press(self, widget, event):
if event.type == gtk.gdk.BUTTON_PRESS:
widget.popup(None, None, None, event.button, event.time)
# Tell calling code that we have handled this event the buck
# stops here.
return True
# Tell calling code that we have not handled this event pass it on.
return False
def button2_press(self, widget, event):
if event.type == gtk.gdk.BUTTON2_PRESS:
widget.popup(None, None, None, event.button, event.time)
return True
return False
def button3_press(self, widget, event):
if event.type == gtk.gdk.BUTTON3_PRESS:
widget.popup(None, None, None, event.button, event.time)
return True
return False
# Print a string when a menu item is selected
def menuitem_response(self, widget, string):
print "%s" % string
def main():
gtk.main()
return 0
if __name__ == "__main__":
MenuExample()
main()
You could do something like this. I'm assuming you just want to execute your .py files, e.g. helloworld.py etc. I'm using Popen from subprocess to execute python (not assuming the py files are executable) scripts. Note that I've edited the script to only have one button, this is just to show you the idea.
import pygtk
pygtk.require('2.0')
import gtk
import subprocess
class Example:
def __init__(self):
window = gtk.Window(gtk.WINDOW_TOPLEVEL)
window.set_size_request(200, 100)
window.set_title("GTK Menu Test")
window.connect("delete_event",
lambda w,e: gtk.main_quit())
vbox = gtk.VBox(False, 0)
window.add(vbox)
vbox.show()
button = gtk.Button("HelloWorld")
button.connect("clicked", self.clicked_helloworld)
vbox.pack_end(button, True, True, 2)
button.show()
window.show_all()
def clicked_helloworld(self, widget):
subprocess.Popen(["python", "helloworld.py"])
def main(self):
gtk.main()
return 0
Example().main()