当你用c编写一个程序时,它有很多printf用于很多目的:调试,信息等。我想在gtk中做类似的事情,有一些小部件可以显示我通常会做的所有printf消息一个普通的 C 程序
谢谢,
---更新---
我按照ptomato的建议使用vte。虽然出现了一些问题。
我使用的是glade-3,当文件glade打开时,vte小部件没有显示。我注意到打开时会显示很多警告和 2 个严重错误。其中关键之一是
Unable to load module 'vte' from any search paths
我通过 apt-get 安装了 libvte-dev 并且知道我在小部件调色板中看到了
When you write a program in c which has a lot of printf for a lot purpose: debuggin, information, etc. I would like to do something like that in gtk, having some widget that would display all the printf messages I usually would do in a normal c program
Thanks,
---UPDATE---
I'm using vte as ptomato advised. Although some problems came up.
I was using glade-3 and when the file glade was open the vte widget didn't show. I notice that a lot of Warning and 2 Critical errors would display while opening. One of those critical was
Unable to load module 'vte' from any search paths
I installed libvte-dev throug apt-get and know i see as in the widget palette
发布评论
评论(4)
您可以使用 VTE,一个终端仿真器小部件。我不确定,但我认为您可以使用
vte_terminal_set_pty()
。You can use VTE, a terminal emulator widget. I'm not sure, but I think you can redirect your program's
stdout
to the widget usingvte_terminal_set_pty()
.您可以创建文本视图或使用标签。
将此文本视图添加到父窗口,例如 Gtk 主窗口。您可以决定添加哪些其他小部件。 (可能是滚动窗口,然后是文本视图)。
每次您想要显示日志时,请使用 api
gtk_text_view_set_buffer
在窗口上显示文本。You can either create a text view or use the label.
Add this text view to the parent window say Gtk Main window. You can decide which other widgets to add. (may be a scroll window and then a text view).
Use the api
gtk_text_view_set_buffer
to display the text on to the window everytime you want to display a log.您应该简单地使用 GtkTextView 并使用这些函数将您的日志重定向到其中:
不幸的是,简单的 printf() 无法轻松重定向(但 g_print() 是一个很好的替代品) 。
PS:抱歉,我只能为您(新用户)粘贴一个链接...
You should simply use a GtkTextView and use these functions to redirect your logging into it:
Unfortunately, the simple printf() cannot be redirected easily (but g_print() is a good replacement for it).
PS: Sorry, I can only paste one link for you (new user)…
您可以使用 asprintf() 作为 GNU 扩展来执行格式化输出,该输出存储在新分配的内存中,并且必须稍后释放。
相反,您可以使用 snprintf() ,它不会为您分配任何内容,它只是为您填充一个字符数组(作为格式化输出)。
您还可以使用 g_string_printf(),它是一个 glib 函数,但它将格式化输出存储到 GString 变量。
当您获得字符串作为格式化输出时,您可以将其用作任何 GTK 小部件的文本。
You can use asprintf() as a GNU extension to perform formatted output which it is stored in a new allocated memory and must be freed later.
Instead that one, you can use snprintf() which didn't allocate anything for you, it just fill an array of chars for you (as a formatted output).
Also you can use g_string_printf() which is a glib function, but it stores the formatted output to a GString variable.
When you got your string as a formatted output, you can use it as a text for any GTK widget.