Hi there friend. I hope you're having a good evening.
Mmm whats what? Software? I might have some around here somewhere.
GTK4 + Python tutorial with code examples
The examples in the README takes the parent widget, but accodring to the docs it just takes a boolean , and I tried it and indeed just using true or false works
WARNING: Before people spend too much effort on this, please read this: https://www.bassi.io/articles/2022/12/02/on-pygobject/
I appreciate your efforts, but be warned.
A few years back when pygtk was the shit, I started writing a few modules to make my work with the gnome desktop more comfortable. Most of the modules are written with PyGObject/GTK 3. This week I came back to maintain it; After some hours of work I stopped. If I find the software really useful I wont write it in Python anymore. Good for prototyping, bad for LTS.
I might be wrong as I am literally only two hours into Python, but the line introduced here seems to be wrong:
c626634#diff-b335630551682c19a781afebcf4d07bf978fb1f8ac04c6bf87428ed5106870f5R297
This results in a compiler error:
TypeError: argument file: Expected Gio.File, but got str
Changing it to css_provider.load_from_path('style.css')
seems to do the trick.
Thank you for this tutorial. Until today I haven't found one for gtk4, so this is a blessing.
Currently I'm struggling with a simple question: how to detect a mouse button click in an Entry?
(I want to open a popup when left-click inside an Entry).
In GTK3, I did (in my entry class):
self.add_events(Gdk.EventMask.BUTTON_PRESS_MASK)
self.connect("button-press-event", self.onClick)
but GTK4 doesn't have "button-press-event", right?
After some search, I managed to do something like this:
self.leftClick = Gtk.GestureClick()
self.leftClick.set_button(1)
self.leftClick.connect("pressed", self.onClick)
self.add_controller(self.leftClick)
but the callback works only when I click on the entry's border, but not in the text field.
It would be nice to know how it works.
I have been following this tutorial to make my side project and it has been very helpful. But the code
if not self.win:
in the "Open with" and single instancing section gave me an error AttributeError: 'App' object has no attribute 'win'.
. My understanding is i should use hasattr()
instead?
"I need help learning how to use icons, titles, background images, and search bars in a Python GTK application. Can you help me find some tutorials on this topic?"
This seems a very important point. This tutorial has been extremely helpful to me thus far!
I'd love to see UI from XML implemented soon.
Hey there,
Would it be possible to add a specific example for events?
That includes buttons - this is easy.
But it would be nice if more events could be showcased, such as
how to respond to mouse-click events in different widgets,
including a gtk entry and a gtk label.
And perhaps a few more examples.
This would be helpful for people who port older code from e. g. gtk3 to see
how things changed in regards to events. The new event model still
confuses me and is one reason I failed to transition into gtk4 so far.
Could your guide briefly show how to install it e. g. the pip command?
I only had it for python gtk3 I think. I am so lazy, I could google but
I prefer it in the guide if that is possible.
Has anyone had success with adding an application icon for Wayland sessions? I'm posting this issue both to ask for help and to show what I've done so far.
GTK4 applications are expected to follow the Freedesktop Icon Theme Specification. Icons should be installed to the icon directory, typically /usr/share/icons
or ~/.local/share/icons
. The icon is supposed to be named according to the application ID, such as com.example.myapp.svg
Then the application icon can be set for a window by using the set_icon_name
method.
my_window = Gtk.Window()
my_window.set_icon_name("com.example.myapp")
However, this doesn't work for all use cases, such as AppImages. When using an AppImage intergration program like AppImageLauncher, icons are saved to the desktop with a hash, using a format similar to appimagekit_3e227aae859f2298ca6d5802f48f6f5b_com.example.myapp.svg
so these icons won't be found using the above code. There is, however, a workaround.
By adapting some code found in this StackExchange question, we can get an icon to show for our application.
icon_theme = Gtk.IconTheme.get_for_display(Gdk.Display.get_default())
icon_theme.add_search_path("/path/to/resource/directory")
This, in addition to the earlier code block for our window, will allow us to add an icon for our application. There's one problem, though. Adding an application icon only seems to work for Xorg sessions. I've been unsuccessful at getting this method to work for Wayland.
I'm honestly a bit stumped with this one. Anyone have a clue as to how to get this working?
This would be extremely helpful as im struggling with playing a mp4 file.
Hi!
Thank you for this amazing repository, it has been incredibly helpful to me.
Could you maybe provide an example on how to use the new GridView? (https://docs.gtk.org/gtk4/class.GridView.html)
It apparently replaces IconView.
I have spent hours but just can't get it to run...
I have some tips / information based on trial and error that I think could help extend this section a little bit.
Firstly, in this part, you say:
'If you were adding a new action icon it would go in /usr/share/icons/hicolor/scalable/actions'
However, this is only good if you're using default icons, and is no good if you have a theme set that's not hicolor.
A better way (to the best of my knowledge) to explain using symbolic icons would be that it will look for an svg matching the
specified svg name, in the currently set theme, so if you have for example a custon svg you want for a button, and your theme is Yaru, you would copy your svg to /usr/share/icons/Yaru/scalable/actions, than reference its name.
You can also replace a button other than a header bar one with an image with set_icon_name.
Adwaita has 'Adw.ButtonContent for adding an icon next to a label, which may also be worth looking into adding if interested.
I hope this was helpful.
Things are going along hunky-dory in this rather nice tutorial until I hit the 'Adding a file chooser' section. I then see the error mentioned in the title. Below is the snippet with the pre and post couple of lines. What am I missing other than the "FileDialog attribiute"? Thanks.
self.open_button.connect('clicked', self.hello)
self.open_button.set_icon_name("document-open-symbolic")
self.open_dialog = Gtk.FileDialog.new()
self.open_dialog.set_title("Select a File")
def show_open_dialog(self, button):
self.open_dialog(self, None, self.open_dialog_open_callback)
It'd be great to add the correct code for radio buttons example into the corresponding source file.
Please add a Gtk.MessageDialog to e.g. display an error or a hint. And close button.
Thanks ...
How to create a ShortcutWindow
and declare shortcuts.
in the 'Better About Window' tab, you showcase some code for an adwaita AboutWindow, but (unless I missed it, and I checked code examples) I can't find a complete usage example of that, and the documentation isn't too much help since it's all for C....
Would you be willing to show an Adw.AboutWindow example that shows where the:
dialog = Adw.AboutWindow(transient_for=app.get_active_window())
and following line(s) are defined?
I was able to make an about window with the section for Gtk.AboutDialouge, but I don't understand the small 'dialog' snippets for the Adw.AboutWindow
Apologies for not providing a patch, but a few points.
rect
s I have to do it differently to make it work. For some reason your method just does not work for me, although it is seemingly the same thing.Graphene.Rect().init(x1, y1, x2, y2)
RoundedRect
section you mention that a radius of 360 makes a circle, as this is per one corner a radius of 90 makes for a circle.Gdk.RGBA
override being restored for GDK4. This allows for Gdk.RGBA(r, g, b, a)
to work again. Also, for some more of these newer types overrides need to be added to make it cleaner to access them.The "Extra Tip: Radio Buttons" contains code that throws error because the self.radio_toggle call is invalid.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.