I have some text in a Gtk.TextView
When I select the text with my mouse or navigate the cursor beyond the bottom edge I cannot se my selection because it does not scroll.
So I think I need to get cursor position and set treeview to this position.
And mouse position while button is clicked and move treeview to this position.
https://lazka.github.io/pgi-docs/Gtk-3.0/classes/TreeView
https://lazka.github.io/pgi-docs/Gtk-3.0/classes/ScrolledWindow
https://lazka.github.io/pgi-docs/Gtk-3.0/classes/Widget
#!/usr/bin/env python
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class ScrolledWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self)
self.set_default_size(200, 200)
self.connect("destroy", Gtk.main_quit)
self.scrolledwindow = Gtk.ScrolledWindow()
self.add(self.scrolledwindow)
box = Gtk.Box()
self.scrolledwindow.add(box)
textview = Gtk.TextView()
box.pack_start(textview, True, True, 10)
textview.connect("move-cursor", self.move_cursor_event) # arrow keys
textview.connect("key-press-event", self.key_press_event) # keyboard input
textview.connect("button-press-event", self.button_press_event) # mouse click
textbuffer = textview.get_buffer()
text = 50*"Hello World\n"
textbuffer.set_text(text, len(text))
def move_cursor_event(self, tree_view, step, count, extend_selection):
print "move_cursor_event", step, count, extend_selection
def key_press_event(self, widget, event):
print "key_press_event", event
def button_press_event(self, widget, event):
print "button_press_event", event.x, event.y
position = self.scrolledwindow.get_vadjustment()
position.set_value(350)
self.scrolledwindow.set_vadjustment(position) #scroll to the middle of the scolled window
window = ScrolledWindow()
window.show_all()
Gtk.main()
Thanks
edit1
here is a very basic solution I found for scrolling the window to the cursor position changed by arrow keys:
def move_cursor_event(self, tree_view, step, count, extend_selection):
rect = tree_view.get_cursor_locations()
self.scroll_y(rect.strong.y)
def scroll_y(self, ypos):
vadj = self.scrolled_window.get_vadjustment()
vadj.set_value(ypos-80)
self.scrolled_window.set_vadjustment(vadj)
The problem here is that textview is put inside a box, which is then put inside self.scrolledwindow. When the TextView is inside a Box, it prevents the ScrolledWindow from recognizing that the TextView needs to be scrolled. Placing the TextView directly in the ScrolledWindow should fix this problem. Here is what the code should look like:
#!/usr/bin/env python
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class ScrolledWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self)
self.set_default_size(200, 200)
self.connect("destroy", Gtk.main_quit)
self.scrolledwindow = Gtk.ScrolledWindow()
self.add(self.scrolledwindow)
textview = Gtk.TextView()
self.scrolledwindow.add(textview)
textbuffer = textview.get_buffer()
text = 50*"Hello World\n"
textbuffer.set_text(text, len(text)) #scroll to the middle of the scolled window
window = ScrolledWindow()
window.show_all()
Gtk.main()
Related
I'm about to create a desktop based widget application using python via the gi.repository and the Gtk/Gdk v3.0. The goal is to have the window taking the whole screen being transparent, frameless and not accepting any focus. In addition it should be shown in every workspace and kept bellow any other window. After spending a long time I figured it out with the following code, yet with a drawback, the window has a vertical white line to the right side (kind of a border). So is there any solution to get rid of this line?
Os: Ubuntu 18.04
Python: 3.6
Screenshot
ui.py
import screeninfo
import gi
try:
gi.require_version("Gtk", "3.0")
gi.require_version("Gdk", "3.0")
gi.require_foreign("cairo")
except ImportError:
print("No Gtk v3.0 or pycairo integration")
from gi.repository import Gtk, Gdk, GLib
class Window (Gtk.Window):
def __init__ (self, canvas):
Gtk.Window.__init__(self)
self.set_skip_pager_hint(True)
self.set_skip_taskbar_hint(True)
self.set_type_hint(Gdk.WindowTypeHint.DESKTOP)
self.set_decorated(False)
self.set_keep_below(True)
self.set_accept_focus(False)
self.set_can_focus(False)
width = 500
height = 500
for monitor in screeninfo.get_monitors():
if monitor.is_primary:
width = monitor.width
height = monitor.height
# Todo: remove the annoying white border line on the right
self.set_size_request(width, height)
#Set transparency
screen = self.get_screen()
rgba = screen.get_rgba_visual()
self.set_visual(rgba)
self.override_background_color(Gtk.StateFlags.NORMAL, Gdk.RGBA(0, 0, 0, 0))
drawingarea = Gtk.DrawingArea()
self.add(drawingarea)
drawingarea.connect('draw', canvas.draw)
self.connect('destroy', Gtk.main_quit)
self.show_all()
self.move(0, 0)
def launch (self):
GLib.timeout_add_seconds(1, self.refresh)
Gtk.main()
return True
def refresh (self):
self.queue_draw()
return True
canvas.py
import cairo
FONT = 'UbuntuCondensent'
SLANT = cairo.FONT_SLANT_NORMAL
BOLD = cairo.FONT_WEIGHT_NORMAL
def draw (da, ctx):
ctx.set_source_rgb(1, 1, 1)
ctx.select_font_face(FONT, SLANT, BOLD)
ctx.set_font_size(32)
ctx.move_to(100, 100)
ctx.text_path('Hi There!')
ctx.fill()
main.py
import signal
import ui
import canvas
def main ():
signal.signal(signal.SIGINT, signal.SIG_DFL)
win = ui.Window(canvas)
win.launch()
if __name__ == '__main__':
main()
I want to get the characters printed only in blue.
How to do it?
Here is the sample program code, which is a fragment of most of the program.
I would be very grateful for your help.
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk
class TextViewWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="awesome gui")
self.set_resizable(True)
self.set_default_size(700, 550)
self.grid = Gtk.Grid()
self.add(self.grid)
self.create_textview()
self.buffer = []
def create_textview(self):
scrolledwindow = Gtk.ScrolledWindow()
scrolledwindow.set_hexpand(True)
scrolledwindow.set_vexpand(True)
self.grid.attach(scrolledwindow, 0, 2, 80, 1)
self.textview = Gtk.TextView()
scrolledwindow.add(self.textview)
self.textbuffer = self.textview.get_buffer()
self.textview.set_editable(False)
self.textview.set_cursor_visible(False)
self.textview.connect("key-press-event", self.on_key_down)
def on_key_down(self, widget, event, data=None):
znak_p = event.string
end_iter_m = self.textbuffer.get_iter_at_line_offset(1, 1)
qwerty_tag = self.textbuffer.create_tag(None, editable=True, foreground="blue")
self.textbuffer.insert_with_tags(end_iter_m, znak_p, qwerty_tag)
win = TextViewWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()
Your on_key_down handler is wrong:
you are creating an anonymous tag every time you're pressing a key
you are using an invalid string for the GtkTextTag:foreground property
you are not returning a value from the callback telling GTK whether you handled the event (and thus should stop the event propagation) or not.
The GtkTextTag:foreground property uses the same format as the gdk_rgba_parse() function; if you want a blue color, you should use rgba(0.0, 0.0, 1.0, 1.0) instead of "blue".
A correct handler is:
def on_key_down(self, widget, event, data=None):
znak_p = event.string
end_iter_m = self.textbuffer.get
self.textbuffer.insert_with_tags(end_iter_m, znak_p, self.qwerty_tag)
return True
Im looking for a way to open a QDialog widget on the center position of the main window.
I have set the position of the mainwindow to center.
centerPoint = qtw.QDesktopWidget().availableGeometry().center()
qtRectangle.moveCenter(centerPoint)
to folow the dialog widget to the postion of the main window
i have set it to
msgb.move(self.pos().x(), self.pos().y())
the dialog window follows the positon of the main window , but it opens on the top left side of the main window, how can I change its position to the center of the main window ?
#!/usr/bin/env python
"""
startscreen
base window remit to specific tests
"""
import os
import sys
from PyQt5 import QtWidgets as qtw
from PyQt5 import QtCore as qtc
class Startscreen(qtw.QWidget):
'''
remit to one of three tests if widgets toggled/clicked
hide its self after
'''
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# your code will go here
# interface
# position
qtRectangle = self.frameGeometry()
centerPoint = qtw.QDesktopWidget().availableGeometry().center()
qtRectangle.moveCenter(centerPoint)
self.move(qtRectangle.topLeft())
# size
self.resize(700, 410)
# frame title
self.setWindowTitle("Lambda")
# heading
heading_label = qtw.QLabel("Lambda Version 1.0")
heading_label.setAlignment(qtc.Qt.AlignHCenter)
# active user
activeuser_label = qtw.QLabel('Benutzer: ' + os.getlogin())
activeuser_label.setStyleSheet("background-color: rgb(234, 246, 22)")
activeuser_label.setAlignment(qtc.Qt.AlignRight | qtc.Qt.AlignTop)
# groubox for widget positioning
self.groupbox = qtw.QGroupBox(self)
# groupbox.setAlignment(qtc.Qt.AlignHCenter)
# layout and widgets
vlayout = qtw.QVBoxLayout()
vlayout.setAlignment(qtc.Qt.AlignHCenter)
self.particlesize_radiobutton = qtw.QRadioButton("test1")
vlayout.addWidget(self.particlesize_radiobutton)
self.dimensionalchange_radiobutton = qtw.QRadioButton("test2")
vlayout.addWidget(self.dimensionalchange_radiobutton)
self.dimensionalchangecook_radiobutton = qtw.QRadioButton("test3")
vlayout.addWidget(self.dimensionalchangecook_radiobutton)
self.select_button = qtw.QPushButton('select')
vlayout.addWidget(self.select_button)
self.groupbox.setLayout(vlayout)
# mainlayout
main_layout = qtw.QFormLayout()
main_layout.addRow(activeuser_label)
main_layout.addRow(heading_label)
main_layout.setVerticalSpacing(40)
main_layout.addRow(self.groupbox)
self.setLayout(main_layout)
# functionality
self.select_button.clicked.connect(self.open_box)
self.show()
def open_box(self):
msgb = qtw.QMessageBox()
msgb.setWindowTitle("title")
msgb.setText("hier sthet was")
msgb.move(self.pos().x(), self.pos().y())
run = msgb.exec_()
# msgb = qtw.QMessageBox()
# msgb.addButton()
# if x open new windwo
#
if __name__ == '__main__':
app = qtw.QApplication(sys.argv)
w = Startscreen()
sys.exit(app.exec_())
A widget has a position relative to its parent, and if it does not have a parent then it will be relative to the screen. And in the case of msgb it belongs to the second case so you will have to convert the coordinate of the center of the window to global coordinates (that is to say with respect to the screen). Even doing the above it will not be centered because the position is with respect to the topleft, that is, the msgb topleft will be in the center of the screen which is not desirable so you have to also take into account the size of the msgb. And the size of the msgb before and after it is displayed is different so with a QTimer it will be enough:
def open_box(self):
msgb = qtw.QMessageBox()
msgb.setWindowTitle("title")
msgb.setText("hier sthet was")
qtc.QTimer.singleShot(
0,
lambda: msgb.move(
self.mapToGlobal(self.rect().center() - msgb.rect().center())
),
)
run = msgb.exec_()
So I have the following code (python 2.7) that checks for a combination of keypresses and shows/hides a gtk window with a few gtk entry boxes. The show/hide works until the window locks up and stops responding, and the inner part of the window turns black. I have to kill the process and start it up again once that window turns black. I've tried all different combinations of show_all() and returning True after hiding the window to no avail. I'm not sure what I'm doing wrong here.
In the end I'd like to be able to press this key combination and show/hide this window without the contents going away.
#!/usr/bin/python
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
import pyxhook
class MyWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Configurator")
self.box = Gtk.Box(spacing=6)
self.add(self.box)
self.ipEntry = Gtk.Entry()
self.ipEntry.set_text("IP Address")
self.maskEntry = Gtk.Entry()
self.maskEntry.set_text("NetMask")
self.gatewayEntry = Gtk.Entry()
self.gatewayEntry.set_text("gatewayEntry")
self.button1 = Gtk.Button(label="Save")
self.button1.connect("clicked", self.on_button1_clicked)
self.box.pack_start(self.ipEntry, True, True, 0)
self.box.pack_start(self.maskEntry, True, True, 0)
self.box.pack_start(self.gatewayEntry, True, True, 0)
self.box.pack_start(self.button1, True, True, 0)
#catch window destroy event and stop it from happening
self.connect('delete-event', self.on_destroy)
def on_button1_clicked(self, widget):
print("Hello")
def on_destroy(self, widget=None, *data):
print("tried to destroy")
self.hide()
return True
#list of ascii keypresses to test if a certain combination of keys is pressed
keypresses = []
win = MyWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
win.set_keep_above(True)
def OnKeyboardEvent(event):
#ascii 227 is l_control, ascii 225 is l_shift, ascii 120 is x
#bring the following external variables into the scope of this function
global keypresses
global win
#check if gtk window is hidden or visible
isVisible = win.get_property("visible")
#store our keypress if it is worthy of being stored (we started with a control character)
if event.Ascii == 227 or ( len(keypresses) >= 1 and keypresses[0] == 227 ):
print("here" + str(len(keypresses)))
keypresses.append(event.Ascii)
#check if we have three items in keypresses to compare, then see if it's the right combination
if len(keypresses) == 3 and keypresses[0] == 227 and keypresses[1] == 225 and keypresses[2] == 120:
#show/hide our window
if isVisible:
win.hide()
del keypresses[:]
keypresses = []
else:
win.show_all()
#clear out our list to compare again
del keypresses[:]
keypresses = []
elif len(keypresses) >= 3:
del keypresses[:]
keypresses = []
#create instance of hook manager
HookManager = pyxhook.HookManager()
#define our callback to fire when a key is pressed
HookManager.KeyDown = OnKeyboardEvent
#hook the keyboard
HookManager.HookKeyboard()
#start our listener
HookManager.start()
Gtk.main()
#close the hook listener when gtk main loop exits
HookManager.cancel()
Here is a small hack that tests the window show/hide when pressing F5. When you press it a second window will show or hide and i've tested for sometime without problems. Maybe it's the HookManager that somehow is messing with the Gtk/Glib mainloop. Nonetheless my method only works if there is a window to grab the key presses, so you will need, indeed, some form of grabbing key presses if your goal is to have no GUI:
#!/usr/bin/python
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import Gdk
#import pyxhook
class MyWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Configurator")
self.box = Gtk.Box(spacing=6)
self.add(self.box)
self.ipEntry = Gtk.Entry()
self.ipEntry.set_text("IP Address")
self.maskEntry = Gtk.Entry()
self.maskEntry.set_text("NetMask")
self.gatewayEntry = Gtk.Entry()
self.gatewayEntry.set_text("gatewayEntry")
self.button1 = Gtk.Button(label="Save")
self.button1.connect("clicked", self.on_button1_clicked)
self.box.pack_start(self.ipEntry, True, True, 0)
self.box.pack_start(self.maskEntry, True, True, 0)
self.box.pack_start(self.gatewayEntry, True, True, 0)
self.box.pack_start(self.button1, True, True, 0)
#catch window destroy event and stop it from happening
self.connect('delete-event', self.on_destroy)
self.connect('key-press-event', self.on_keypress)
def on_keypress(self, widget, event):
global w
if event.keyval == Gdk.KEY_F5:
isVisible = w.get_property("visible")
if (isVisible):
w.hide()
else:
w.show()
print("Keypress")
def on_button1_clicked(self, widget):
print("Hello")
def on_destroy(self, widget=None, *data):
print("tried to destroy")
self.hide()
return False
w = MyWindow()
win = MyWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
win.set_keep_above(True)
Gtk.main()
I can Add boxs and Tables
Also the Quit button does not work can someone edit that part of the code please
import gtk
class helloworld:
def close(self,widget):
print "I'm outta here"
gtk.main_quit()
def printit(self,widget,lab1):
print lab1.get_text()
def filllab(self,widget,lab1):
lab1.set_text(widget.get_text())
def __init__(self):
window = gtk.Window()
window.set_size_request(300,400)
vbox = gtk.VBox()
window.add(vbox)
lab1 = gtk.Label("shazbut")
# entry widget
ent1 = gtk.Entry()
ent1.connect("activate",self.filllab,lab1)
# quit
quitb = gtk.Button("quit",gtk.STOCK_QUIT)
quitb.set_size_request(50,100)
quitb.connect("destroy", gtk.main_quit)
printb = gtk.Button("print")
printb.connect("clicked",self.printit,lab1)
# Pack widgets in the vbox
vbox.add(ent1)
vbox.add(lab1)
vbox.add(quitb)
vbox.add(printb)
window.show_all()
helloworld()
gtk.main()
The "quit" button doesn't work because you connected the "destroy" signal to the button instead of the clicked signal
quitb.connect("clicked", gtk.main_quit)
Also, you forgot to connect the window destroy event (so the program will never exit when you click the window close button). Add
window.connect("destroy", gtk.main_quit)
To change the label properties use the pango attributes
def _add_attributes_to_label(self,label):
attr = pango.AttrList()
fg_color = pango.AttrForeground(65535, 0, 0,0,-1)
strike = pango.AttrStrikethrough(True,0,-1)
size = pango.AttrSize(30000, 0, -1)
attr.insert(fg_color)
attr.insert(size)
attr.insert(strike)
label.set_attributes(attr)
In your init function call the previous function like this:
self._add_attributes_to_label(lab1)
Follow this tutorial to know more about pango attributes.