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
Related
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()
set_attached_to seems to be the correct way to position a popup window relative to a widget in wayland:
Examples of places where specifying this relation is useful are for instance [...] a completion popup window created by Gtk.Entry [...]
Unfortunately this only yields an error
Gdk-Message: 12:13:16.143: Window 0x1822340 is a temporary window without parent, application will not be able to position it on screen.
Trying to uncomment the popup.set_parent(entry) line only adds a warning:
(try_entry_popup.py:4539): Gtk-WARNING **: 12:17:34.185: Can't set a parent on a toplevel widget
followed by the same error.
Here is a minimal example:
#!/usr/bin/env python
# stripped down from https://gitlab.gnome.org/GNOME/gtk/issues/1541#note_396391
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
entry = Gtk.Entry()
popup = Gtk.Window(type=Gtk.WindowType.POPUP)
#popup.set_parent(entry)
popup.set_attached_to(entry)
popup.show_all()
layout = Gtk.VBox()
layout.pack_start(entry, False, True, 0)
window = Gtk.Window()
window.connect("destroy", Gtk.main_quit)
window.add(layout)
window.show_all()
Gtk.main()
From the entry completion source it looks like it definitely should work.
Is it using private features ? Or what am I missing ?
Well, not really: gtk_window_set_attached_to has nothing to do with positioning, it's important for accessibility (a11y) and to apply theming in a correct way. If you want to position your popup window you can follow what it's done in https://gitlab.gnome.org/GNOME/gtk/blob/075dcc142aa525778268165095de019b736f3efa/gtk/gtkentrycompletion.c#L1597
Here's a very simple implementation:
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
def on_button_clicked(widget):
popup = Gtk.Window(type=Gtk.WindowType.POPUP)
# optionally you may set an appropriate type hint, but it's not required.
popup.set_attached_to(entry)
popup.set_transient_for(window)
gdk_window = entry.get_window()
gdk_window_origin = gdk_window.get_origin()
x = gdk_window_origin[1]
y = gdk_window_origin[2]
allocation = entry.get_allocation()
x += allocation.x
y += allocation.y + allocation.height
popup.move(x, y)
popup.show_all()
button = Gtk.Button()
button.connect('clicked', on_button_clicked)
entry = Gtk.Entry()
layout = Gtk.VBox()
layout.pack_start(button, False, True, 0)
layout.pack_start(entry, False, True, 0)
window = Gtk.Window()
window.connect("destroy", Gtk.main_quit)
window.add(layout)
window.show_all()
Gtk.main()
When I add a page to a Gtk.Notebook programatically, I can't find a way to set the tab to expand. The property is available in Glade, so I know it can be done. Here is a snippet using reorderable and detachable properties.
#!/usr/bin/env python
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class GUI:
def __init__(self):
window = Gtk.Window()
notebook = Gtk.Notebook()
window.add(notebook)
for i in range (4):
label = Gtk.Label('label in page number ' + str(i))
tab_label = Gtk.Label('page ' + str(i))
notebook.append_page(label, tab_label)
notebook.set_tab_detachable(label, True)
notebook.set_tab_reorderable(label, True)
window.show_all()
window.connect('destroy', Gtk.main_quit)
app = GUI()
Gtk.main()
However,
notebook.set_tab_expandable(label, True)
fails with
AttributeError: 'Notebook' object has no attribute 'set_tab_expandable'
The solution is:
notebook.child_set_property(label, 'tab-expand', True)
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
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).