gpt4 book ai didi

c - 如何从 gtk_entry 设置变量值?

转载 作者:行者123 更新时间:2023-11-30 17:32:10 32 4
gpt4 key购买 nike

我想从条目中设置 var pid,但收到以下警告:

gtk.c:7:6: warning: assignment makes integer from pointer without a cast [enabled by default].

如何创建 var pid == 条目?这是非常重要的,因为这个程序将向我们要输入 id (pid) 的进程发送信号。

#include <gtk/gtk.h>
#include <stdlib.h>

pid_t pid;
void sendSighup(GtkWidget *widget,GtkWidget *entry, gpointer label) {

pid = gtk_entry_get_text(GTK_ENTRY(entry));
kill(pid,SIGHUP);
}
void sendSigint(GtkWidget *widget, gpointer label) {


kill(pid,SIGINT);
}
void sendSigkill(GtkWidget *widget, gpointer label) {

kill(pid,SIGKILL);
}
void sendSigterm(GtkWidget *widget, gpointer label) {

kill(pid,SIGTERM);
}
void sendSigstop(GtkWidget *widget, gpointer label) {

kill(pid,SIGSTOP);
}

accept_clicked (GtkButton *button, GObject *context_object)
{
GtkLabel *accept_lable1 = g_object_get_data (context_object, "label1");
GtkEntry *accept_entry = g_object_get_data (context_object, "entry");

const char *entry_in = gtk_entry_get_text (accept_entry);
gtk_label_set_text (accept_lable1, entry_in);
}



int main( int argc, char *argv[])
{
GtkWidget *window;
GtkWidget *frame;
GtkWidget *table;

GtkWidget *button1;
GtkWidget *button2;
GtkWidget *button3;
GtkWidget *button4;
GtkWidget *button5;
GtkWidget *quit;
GtkWidget *set;
GtkWidget *vseparator;
GtkWidget *entry;
GtkWidget *label;
GtkWidget *label1;
GtkWidget *label2;

gtk_init(&argc, &argv);

window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "Send Signal");
gtk_window_set_default_size(GTK_WINDOW(window), 300, 250);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);

frame = gtk_fixed_new();
gtk_container_add(GTK_CONTAINER(window), frame);

button1 = gtk_button_new_with_label("Sighup");
gtk_fixed_put(GTK_FIXED(frame), button1, 8, 15);
gtk_widget_set_size_request(button1, 80, 35);

button2 = gtk_button_new_with_label("Sigint");
gtk_fixed_put(GTK_FIXED(frame), button2, 8, 60);
gtk_widget_set_size_request(button2, 80, 35);

button3 = gtk_button_new_with_label("Sigkill");
gtk_fixed_put(GTK_FIXED(frame), button3, 8, 105);
gtk_widget_set_size_request(button3, 80, 35);

button4 = gtk_button_new_with_label("Sigterm");
gtk_fixed_put(GTK_FIXED(frame), button4, 8, 150);
gtk_widget_set_size_request(button4, 80, 35);

button5 = gtk_button_new_with_label("Sigstop");
gtk_fixed_put(GTK_FIXED(frame), button5, 8, 195);
gtk_widget_set_size_request(button5, 80, 35);

set = gtk_button_new_with_label ("Set");
gtk_fixed_put(GTK_FIXED(frame), set, 200, 80);
gtk_widget_set_size_request(set, 80, 35);

quit = gtk_button_new_with_label("Quit");
gtk_fixed_put(GTK_FIXED(frame), quit, 200, 195);
gtk_widget_set_size_request(quit, 80, 35);

vseparator = gtk_vseparator_new();
gtk_widget_set_size_request(vseparator, 10, 240);
gtk_fixed_put(GTK_FIXED(frame), vseparator, 95, 5);

label = gtk_label_new("Enter PID:");
gtk_fixed_put(GTK_FIXED(frame), label, 165, 20);

label2 = gtk_label_new("PID:");
gtk_fixed_put(GTK_FIXED(frame), label2, 110, 89);

label1 = gtk_label_new("0000");
gtk_fixed_put(GTK_FIXED(frame), label1, 140, 89);

entry = gtk_entry_new();
gtk_fixed_put(GTK_FIXED(frame), entry, 120, 40);
gtk_widget_grab_focus(entry);

g_object_set_data(G_OBJECT(set), "label1", label1);
g_object_set_data(G_OBJECT(set), "entry", entry);

g_signal_connect(GTK_BUTTON (set), "clicked",
G_CALLBACK (accept_clicked), set);

g_signal_connect_swapped(G_OBJECT(window), "destroy",
G_CALLBACK(gtk_main_quit), NULL);

g_signal_connect(button1, "clicked",
G_CALLBACK(sendSighup), NULL);

g_signal_connect(button2, "clicked",
G_CALLBACK(sendSigint), NULL);

g_signal_connect(button3, "clicked",
G_CALLBACK(sendSigkill), NULL);

g_signal_connect(button4, "clicked",
G_CALLBACK(sendSigterm), NULL);

g_signal_connect(button5, "clicked",
G_CALLBACK(sendSigstop), NULL);


g_signal_connect(G_OBJECT(quit), "clicked",
G_CALLBACK(gtk_main_quit), G_OBJECT(window));


gtk_widget_show_all(window);
gtk_widget_show(entry);
gtk_widget_show(label);

gtk_main();

return 0;
}

最佳答案

在该行中,您可以使用普通的旧atoi:

pid = atoi(gtk_entry_get_text(GTK_ENTRY(entry)));

或使用更复杂的函数,例如 g-ascii-strtod .

关于c - 如何从 gtk_entry 设置变量值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24326778/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com