Python GTK+ 3 - Can't set a parent on a toplevel widget - python

Gtk-WARNING **: 19:18:52.313: Can't set a parent on a toplevel widget
Why do I get this warning? Isn't master win a toplevel widget? What am I doing wrong?
Shouldn't master_win be toplevel? It is created first...
class PluginWindow(Gtk.Window):
def __init__(self):
super().__init__(title="Stack Demo")
#some stuff
master_win = Gtk.ScrolledWindow()
master_win.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.ALWAYS)
win = PluginWindow()
master_win.add(win)
win.connect("destroy", Gtk.main_quit)
master_win.show_all()
Gtk.main()

You have the order wrong
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
class PluginWindow(Gtk.Window):
def __init__(self):
super().__init__(title="Stack Demo")
#some stuff
master_win = Gtk.ScrolledWindow()
master_win.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.ALWAYS)
win = PluginWindow()
win.add(master_win)
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()

You're trying to add a Gtk.Window() object into Gtk.ScrolledWindow() object and that is not possible. It's inversed.
A Gtk.Window is a toplevel window which can contain other widgets. Windows normally have decorations that are under the control of the windowing system and allow the user to manipulate the window (resize it, move it, close it,…).
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
class PluginWindow(Gtk.Window):
def __init__(self):
super().__init__(title="Stack Demo")
# some stuff
master_win = Gtk.ScrolledWindow()
master_win.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.ALWAYS)
win = PluginWindow()
# Gtk.ScrolledWindow added to Gtk.Window
win.add(master_win)
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()

Related

How to Pygtk WindowTypeHint to DOCK on XFCE and have it visible on the desktop

So I've run into an issue where whenever I set the WindowTypeHint to anything other than normal the window just disappears. I've verified the hint type with print.Below is my sample code
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk
# Create the main window
class MyWindow(Gtk.Window):
def __init__(self):
super().__init__(title="Hello World")
# Innitially setting the bar to off
self.set_decorated(False)
# Attempting to set window type to prevent minimizing when show desktop is hit and to stay behind other objects etc.
self.set_type_hint(Gdk.WindowTypeHint.DOCK)
# Defining the button and drawing it
self.button = Gtk.Button(label="Click Here")
self.button.connect("clicked", self.on_button_clicked)
self.add(self.button)
# define the button functions
def on_button_clicked(self, widget):
if win.props.decorated == False:
win.set_decorated(True)
else:
win.set_decorated(False)
print("Hello World")
print(win.props.decorated)
print(self.props.type_hint)
# Set alias for the window
win = MyWindow()
# Testing. Print the hint type to console
print(win.props.type_hint)
# Window settings.
win.set_keep_below(True)
win.connect("destroy", Gtk.main_quit)
win.show_all()
print(win.props.type_hint)
Gtk.main()
Window to appear as a dock on the desktop where the mouse pointer is or in the corner of one of the monitors the same way glava works with it's settings.
Item was drawing off screen. Multiple monitors had it throwing the item into the corner off screen between them. If anyone else comes across this I resolved it by setting the coordinates with self.move(x, y) in the initialisation. Found where I wanted it to root with win.get_position() on button press to find where I wanted it.

PyGTK entry.set_placeholder_text doesnt work - missing tex

I am attaching a piece of my code in which I try to display the text in the Entry field with predefined text. I use entry.set_placeholder_text for this, but unfortunately the text does not appear.
Is there an effective solution?
# -*- coding: utf-8 -*-
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
class MyWindow(Gtk.Window):
def __init__(self):
super(MyWindow, self).__init__()
entry = Gtk.Entry()
entry.set_placeholder_text("Any text")
self.add(entry)
win = MyWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()
The placeholder text is only visible when the entry is empty and unfocused. Adding another widget that can get the focus will make the text appear.
Example:
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
class MyWindow(Gtk.Window):
def __init__(self):
super(MyWindow, self).__init__()
entry = Gtk.Entry()
entry.set_placeholder_text("Any text")
box = Gtk.HBox()
self.add(box)
button = Gtk.Button()
box.pack_start(button, False, False, 0)
box.pack_start(entry, True, True, 0)
win = MyWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()
I know, I have no idea why it doesn't work.
I usually do:
search_entry.props.placeholder_text = 'Placeholder text'
before calling set_active on the widget

How to make the window behind a dialog not clickable?

i want to set blocked the window behind dialog to prevent the user do click or modify any content of the window while the dialog are running, and when the user close de dialog then set unlocked the window behind dialog.
import gtk;
window = gtk.Window();
window.set_title("Window Behind Dialog");
window.set_default_size(426,240);
textentry = gtk.TextView();
window.add(textentry);
window.show_all();
dialog = gtk.Window();
dialog.set_title("Dialog");
dialog.set_default_size(256,144);
label = gtk.Label("Unlock the window behind when this dialog get close");
dialog.add(label);
dialog.show_all();
gtk.main();
Which method is used for it, in Gtk or PyGtk?, for example:
window.set_disabled_to_all_events();
or
window.set_disabled();
or
window.events_disabled(True);
or
window.set_blocked(True);
If you have a window manager that honors modal windows, you could use set_modal on the dialog window.
If not, you could use set_sensitive on the parent window. Call this with False when the dialog is shown, and with True when the dialog is hidden or destroyed.
I've added Gtk3 examples below. I recommend that you switch to PyGObject and Python 3 before investing too much effort in a deprecated toolkit.
Modal window example:
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
window = Gtk.Window(title="Hello World")
window.connect("destroy", Gtk.main_quit)
window.add(Gtk.TextView())
window.show_all()
dialog = Gtk.Window(title="Dialog")
dialog.set_transient_for(window)
dialog.set_modal(True)
dialog.show()
Gtk.main()
Or using explicit set_sensitive:
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
window = Gtk.Window(title="Hello World")
window.connect("destroy", Gtk.main_quit)
window.add(Gtk.TextView())
window.show_all()
dialog = Gtk.Window(title="Dialog")
dialog.set_transient_for(window)
window.set_sensitive(False)
def destroy_cb(widget, data):
data.set_sensitive(True)
dialog.connect("destroy", destroy_cb, window)
dialog.show()
Gtk.main()

Python Gtk Tutorial

This is example 2 from the GTK tutorial
from gi.repository import Gtk
class MyWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Hello World")
self.button = Gtk.Button(label="Click Here")
self.button.connect("clicked", self.on_button_clicked)
self.add(self.button)
def on_button_clicked(self, widget):
print("Hello World")
win = MyWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
When I attempt to run, it displays nothing and when I close the window, I receive a message saying the program is still running. I've stripped down the code and by eliminating the buttons, the window will appear so I think that there is a mistake in the button.add.
In your code there was an unexpected indent error from the line containing 'class' and it didn't use the __name__ == '__main__' trick (though that's just a matter of good habit).
This should work. At least it does for Gtk+3 with Python 3.4 on my Ubuntu dist.
from gi.repository import Gtk
class MyWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Hello World")
self.button = Gtk.Button(label="Click Here")
self.button.connect("clicked", self.on_button_clicked)
self.add(self.button)
def on_button_clicked(self, widget):
print("Hello World")
if __name__ == '__main__':
win = MyWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
Try to import from this way
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class MyWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Hello World")
self.button = Gtk.Button(label="Click Here")
self.button.connect("clicked", self.on_button_clicked)
self.add(self.button)
def on_button_clicked(self, widget):
print("Hello World")
win = MyWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
Then use your ide for reindent all lines
if not work uninstall all your gtk modules, install this
https://sourceforge.net/projects/pygobjectwin32/files/
and try again

Vertical Scroll Bar in PyGTK

I made a combined text editor-terminal in Python using GTK. I want users to be able to control the amount of vertical space each occupies in the same window. I don't know how to do that, I need help, the suggested solutions I've searched online have not worked.
# UI
# Imort modules
import os
from gi.repository import Gtk, Vte
from gi.repository import GLib
from gi.repository import Gtk
class MainWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self)
# Window title and Icon
self.set_title("MaeTrics")
# Vertical Box
self.box = Gtk.VBox(homogeneous=False, spacing=0)
self.add(self.box)
# Scrolled Text Window
scrolledwindow1 = Gtk.ScrolledWindow()
scrolledwindow1.set_hexpand(True)
scrolledwindow1.set_vexpand(True)
self.textview = Gtk.TextView()
self.textbuffer = self.textview.get_buffer()
scrolledwindow1.add(self.textview)
# Terminal
# scrolledwindow2 = Gtk.ScrolledWindow()
# scrolledwindow2.set_hexpand(True)
# scrolledwindow2.set_vexpand(True)
terminal = Vte.Terminal()
terminal.fork_command_full(Vte.PtyFlags.DEFAULT,os.environ['HOME'],["/bin/sh"],[],GLib.SpawnFlags.DO_NOT_REAP_CHILD,None,None,)
# scrolledwindow2.add(terminal)
# Pack everything in vertical box
self.box.pack_start(scrolledwindow1, True, True, 0)
self.box.pack_start(terminal,True,True,0)
# Callback functions
self.connect("delete-event", Gtk.main_quit)
self.show_all()
window = MainWindow()
Gtk.main()
You want GtkPaned for this. Set the orientation to GTK_ORIENTATION_VERTICAL for a vertical one, put the text editor scrolled window in the first slot (gtk_paned_add1), and the terminal scrolled window in the second slot (gtk_paned_add2).

Categories