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).
Related
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.
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
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()
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()