


php editor Zimo is happy to answer a frequently asked question: "Can you add a GTK4 applicationWindow from a ui file to a GTK4 application?" The answer is yes! In GTK4, you can use GtkBuilder to load the UI file and add the applicationWindow widget in it to your GTK application. In this way, you can design and manage the application interface more conveniently and improve development efficiency. However, please note that to successfully load the UI files, you need to ensure that the relevant libraries and tools for GTK4 are installed in your system. Hope this answer helps you!
Question content
I have only been using GTK for a few days and this is a newbie question since my go program requires a GUI. Being a newbie, I took the easiest way I could find to get started using Cambalache to generate a .UI file and then call it in my go application.
I put everything under ApplicationWindow in the .UI file and let it load and work fine with gkt4-builder-tool but in my go app I don't have events in the GUI in the program. Looking at the sample code, I found that I needed to attach the ApplicationWindow to the GTK application instantiated in my go program, and I changed the >ApplicationWindow by just A window and it works just fine since I can add a normal window to the GTK application.
The problem is that I can't find a way to extract the ApplicationWindow from the UI file and then attach it to the application. The only way to attach a window to an application is app.AddWindow() but it only adds GtkWindow, not GtkApplicationWindow and in a strongly typed language like go It’s “no go” (no pun intended).
You can export your GTK application directly into a .UI file along with ApplicationWindow and in theory if you can do this successfully you could extract them both, maybe via this way to connect them, however, I have not successfully exported the GTK application, ApplicationWindow and menu because gkt4-builder-tool keeps getting validation errors (after trying multiple configurations ), so Cambalache doesn't know how to export that combination; or I don't know how to get it to do so; or it's not actually a legal combination in the .UI file.
So my question is, should I give up trying to store the ApplicationWindow in the .UI file and just build the ApplicationWindow widget in code, or is there any other option? Haven't learned it yet?
If it's not really necessary, I could ditch ApplicationWindow and use Windows as another path.
Thank you for your knowledge and experience.
Thanks!
renew:
More clear based on Kripto's comments.
This is the code. This won't run directly because I extracted the relevant snippet in main() from a larger program, and env, log := boot.Initialize()
doesn't exist in this snippet, but It is not important to understand the problem.
I know there is only one application window in the GTK GUI. The concept is similar, if not identical, to the Application Window from my old Visual Basic days.
The following is the go code and a condensed version containing the relevant elements in the .UI file:
If I change line 8 in the .UI file:
<object class="GtkApplicationWindow" id="appWin"></object>
Regarding:
<object class="GtkWindow" id="appWin"></object>
Go Code will open the .UI file and it works, but now I don't have the application window.
If I leave line 8 in the .UI file as is, then it still opens the file but the resulting GUI is non-interactive and only when I stop my running Go program It will only turn off running in debug mode in GoLand. This is because to run it I had to make two changes to the go program. :
- Change this line:
appWindow := builder.GetObject(
appWin).Cast().(*gtk.Window)
to
appWindow := builder.GetObject(
appWin).Cast().(*gtk.ApplicationWindow)
- Comment out this line:
app.AddWindow(appWindow)
The second one is the problem because now the window is not attached to the application and I'm sure that's why it's non-interactive.
package main import ( "os" "github.com/diamondburned/gotk4/pkg/gio/v2" "github.com/diamondburned/gotk4/pkg/gtk/v4" "gitlab.com/trading5124936/core.git/loggers" ) func main() { env, log := boot.Initialize() var app *gtk.Application app = gtk.NewApplication(`site.TradingAnalyzer`, gio.ApplicationFlagsNone) app.ConnectActivate(func() { activate(app, log, env.Paths.GUIFile) }) if code := app.Run(os.Args); code > 0 { os.Exit(code) } } func activate(app *gtk.Application, log *loggers.Logger, guiPath string) { // You can build UIs using Cambalache (https://flathub.org/apps/details/ar.xjuan.Cambalache) b, err := os.ReadFile(guiPath) if err != nil { log.Critical(err) return } uiXML := string(b) builder := gtk.NewBuilderFromString(uiXML, len(uiXML)) // MainWindow and Button are object IDs from the UI file appWindow := builder.GetObject(`appWin`).Cast().(*gtk.Window) entry := builder.GetObject(`GeneralSetup.Timezone`).Cast().(*gtk.Entry) entry.Connect("changed", func() { println(`Changed`) }) app.AddWindow(appWindow) appWindow.Show() }
This is the .UI file:
<?xml version='1.0' encoding='UTF-8'?> <!-- Created with Cambalache 0.16.0 --> <interface domain="ta.site"> <!-- interface-name TradingAnalyzer.ui --> <!-- interface-authors Reg Proctor --> <requires lib="gtk" version="4.6"/> <object class="GtkApplication" id="app"/> <object class="GtkApplicationWindow" id="appWin"> <property name="default-height">925</property> <property name="default-width">1200</property> <child> <object class="GtkPaned"> <child> <object class="GtkFrame"> <property name="label">Settings</property> <child> <object class="GtkStackSidebar"> <property name="halign">start</property> <property name="height-request">900</property> <property name="stack">pages</property> <property name="valign">start</property> </object> </child> </object> </child> <child> <object class="GtkFrame"> <child> <object class="GtkStack" id="pages"> <property name="name">Timezone</property> <child> <object class="GtkStackPage" id="GeneralSetup"> <property name="child"> <object class="GtkFlowBox"> <property name="margin-bottom">20</property> <property name="margin-end">50</property> <property name="margin-start">50</property> <property name="margin-top">20</property> <property name="name">Timezone</property> <child> <object class="GtkEntry" id="GeneralSetup.Timezone"> <property name="activates-default">True</property> <property name="halign">start</property> <property name="height-request">10</property> <property name="input-purpose">alpha</property> <property name="placeholder-text">Timezone</property> <property name="text">America/Phoenix</property> <property name="tooltip-text">Enter your timezone</property> <property name="valign">center</property> <property name="width-request">50</property> </object> </child> </object> </property> <property name="name">General Setup</property> <property name="title">General Setup</property> </object> </child> </object> </child> </object> </child> </object> </child> </object> </interface>
Update 2
我找到了部分答案。您可以做到这一点,但您需要将 App 添加到 ApplicationWindow 中,而不是相反,因此这是一个不同的功能。这是它的工作方式(它也有点短,因为我了解到有一个从文件加载的函数):
func activate(app *gtk.Application, log *loggers.Logger, guiPath string, appName string) { const ext = `.ui` guiPath += `/` builder := gtk.NewBuilderFromFile(guiPath + appName + ext) // builder := gtk.NewBuilderFromFile(guiPath + `Another template File.ui`) appWindow := builder.GetObject(`appWin`).Cast().(*gtk.ApplicationWindow) entry := builder.GetObject(`GeneralSetup.Timezone`).Cast().(*gtk.Entry) entry.Connect("changed", func() { println(`Changed`) }) // ***** THE LINE THAT MAKES THE DIFFERENCE *** appWindow.SetApplication(app) appWindow.Show() }
我仍然不确定是否存在将应用程序对象导出到这些 .UI 文件之一的情况。
我还没有找到任何人这样做的例子,我倾向于相信这不是你应该做的,但我仍在学习,所以很容易出错。
解决方法
更新 2 提供了此问题的大部分答案。
此外,到目前为止,我认为没有任何理由将应用程序添加到 UI 文件中。无论如何,它往往会抛出错误。
The above is the detailed content of Can you add a GTK4 applicationWindow from a ui file to a GTK4 application?. For more information, please follow other related articles on the PHP Chinese website!

This article explains Go's package import mechanisms: named imports (e.g., import "fmt") and blank imports (e.g., import _ "fmt"). Named imports make package contents accessible, while blank imports only execute t

This article details efficient conversion of MySQL query results into Go struct slices. It emphasizes using database/sql's Scan method for optimal performance, avoiding manual parsing. Best practices for struct field mapping using db tags and robus

This article explains Beego's NewFlash() function for inter-page data transfer in web applications. It focuses on using NewFlash() to display temporary messages (success, error, warning) between controllers, leveraging the session mechanism. Limita

This article explores Go's custom type constraints for generics. It details how interfaces define minimum type requirements for generic functions, improving type safety and code reusability. The article also discusses limitations and best practices

This article demonstrates creating mocks and stubs in Go for unit testing. It emphasizes using interfaces, provides examples of mock implementations, and discusses best practices like keeping mocks focused and using assertion libraries. The articl

This article details efficient file writing in Go, comparing os.WriteFile (suitable for small files) with os.OpenFile and buffered writes (optimal for large files). It emphasizes robust error handling, using defer, and checking for specific errors.

The article discusses writing unit tests in Go, covering best practices, mocking techniques, and tools for efficient test management.

This article explores using tracing tools to analyze Go application execution flow. It discusses manual and automatic instrumentation techniques, comparing tools like Jaeger, Zipkin, and OpenTelemetry, and highlighting effective data visualization


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

SublimeText3 Mac version
God-level code editing software (SublimeText3)

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver CS6
Visual web development tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
