LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
12
返回列表 发新帖
楼主: dyte200

gtk的entry怎样实时显示?

[复制链接]
发表于 2006-1-16 16:02:38 | 显示全部楼层
我试过了,example在我机上没问题,能正确的运行
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-1-16 21:46:34 | 显示全部楼层
Post by Linux_Lyb
我试过了,example在我机上没问题,能正确的运行

你的编译参数是什么?
回复 支持 反对

使用道具 举报

发表于 2006-1-17 15:16:48 | 显示全部楼层
gtk需要一个事件循环才能处理所做的事情,这样的情况我遇到过,用线程的话也会出现类似的问题!!
具体用什么函数我忘记了!!gdk_flush ()等差不多!!

记得那会我们好像把xlib的函数都用上了!!
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-1-17 19:28:27 | 显示全部楼层
今天试了一下,gtk下如果用fork产生进程,在新进程里对entry值进行更新一点办法也没有了。包括信号中断的方法也无能为力。
回复 支持 反对

使用道具 举报

发表于 2006-1-18 09:42:41 | 显示全部楼层
Post by dyte200
你的编译参数是什么?

gcc -o test test.c `pkg-config gtk+-2.0 gthread-2.0 --libs --cflags`

你贴出源代码或许有帮助。

13楼提到的gdk_flush()好像对entry没什么作用,用在label上倒可以,也许我不知道entry上怎么用 :confused: ,13楼提到方法是:
gtk_entry_set_text()
gdk_window_process_updates () 或 gdk_window_process_all_updates ()
gdk_flush ()

附上我的一个实例
[PHP]
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <gtk/gtk.h>
#include <glib.h>
#include <pthread.h>

GtkWidget *win,*entry;
int i;
gchar str[255];
guint timer;

void *updata(void *args)
{
        while (1) {
                sleep(1);
                if (i < 10) ++i; else i = 0;
                sprintf (str,"n: %d",i);

                gdk_threads_enter();
                gtk_entry_set_text (GTK_ENTRY(entry),str);
                gdk_threads_leave();       
               
        }
}

int main (int argc,char *argv[]) {
        pthread_t tid;
        g_thread_init (NULL);
          gdk_threads_init ();

        gtk_init (&argc,&argv);

        win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
        g_signal_connect(G_OBJECT(win),"delete_event",

                        G_CALLBACK(gtk_main_quit),NULL);

        entry = gtk_entry_new();
        gtk_container_add (GTK_CONTAINER(win),entry);
        gtk_widget_show (entry);

        gtk_widget_show (win);
        gtk_entry_set_text (GTK_ENTRY(entry),"hello world");
        i = 0;

        pthread_create (&tid, NULL, updata,NULL);

        /* enter the GTK main loop */
          gdk_threads_enter ();
          gtk_main ();
          gdk_threads_leave ();

        return 0;
}
[/PHP]
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表