草庐IT

c - gtk_entry_get_text 不工作

coder 2024-06-16 原文

我在 c 中使用 GTK 2.2。我可以从其中一个条目中获取文本并使用类型转换将其转换为整数,但它不适用于第二个条目。由于某种原因,我设置为从第二个条目获取文本的变量保持为 0。这是我的代码。 button2_clicked 部分不工作,但问题也可能出在 button2 g_signal 连接上。

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

int prod,prod_amt,amt,amt_holder,tot_amt;

static void button3_clicked(GtkWidget *widget,gpointer window){
    GtkWidget *dialog;

    dialog = gtk_message_dialog_new (GTK_WINDOW (window), GTK_DIALOG_MODAL,GTK_MESSAGE_INFO, GTK_BUTTONS_CLOSE, "The Total amount is %d",tot_amt);
    gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_CENTER);
    gtk_dialog_run (GTK_DIALOG (dialog));
    gtk_widget_destroy (dialog);
}

static void button2_clicked(GtkWidget *widget,gpointer data){
    amt = (int)gtk_entry_get_text(GTK_ENTRY(data));
    prod_amt = prod;
    int products[10];
    products[0] = 15;
    products[1] = 200;
    products[2] = 55;
    products[3] = 30000;
    products[4] = 15000;
    products[5] = 20000;
    products[6] = 20;
    products[7] = 30;
    products[8] = 2500;
    products[9] = 1000;
    if(amt>0 && amt<11)
    amt_holder = products[amt-1];
    tot_amt += amt_holder;
    g_print("%d",tot_amt);
    gtk_entry_set_text(GTK_ENTRY(data),"");
}

static void prod_list (GtkWidget *widget, GtkWidget *window)
{
    GtkWidget *dialog;
    dialog = gtk_message_dialog_new (GTK_WINDOW (window), GTK_DIALOG_MODAL,     GTK_MESSAGE_INFO, GTK_BUTTONS_CLOSE, "Kurkure 15,Chocolate Pack 200\n Dove Shampoo 55     Laptop 30,000\n Television 15,000 Netbook 20,000\n Oreo 20 Dark Fantasy 30\n Toaster 2500 Clothes 1000");
    gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_CENTER);
    gtk_dialog_run (GTK_DIALOG (dialog));
    gtk_widget_destroy (dialog);
}

static void button_clicked(GtkWidget *widget,gpointer data){
    prod = (int)gtk_entry_get_text(GTK_ENTRY(data));
    g_print(prod);
    gtk_entry_set_text(GTK_ENTRY(data),"");
}

static void clear_clicked(GtkWidget *widget,gpointer data){
    gtk_entry_set_text(GTK_ENTRY(data),"");
}

int main(int argc,char* argv) {
    GtkWidget *window,*button,*entry,*table,*dialog,*clearb;
    GtkWidget *entry2,*button2,*listofprod,*button3;
    gtk_init(&argc,&argv);
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    float product[5],no_of_products,amount,total_amount=0,product_holder,amount_holder=0;
    button = gtk_button_new_with_mnemonic("Enter number of products");
    entry = gtk_entry_new();
    button2 = gtk_button_new_with_mnemonic("Enter Product Numbers");
    entry2 = gtk_entry_new();
    table = gtk_table_new(3,3,0);
    clearb = gtk_button_new_with_label("Clear");
    button3 = gtk_button_new_with_mnemonic("Total amount");
    listofprod = gtk_button_new_with_mnemonic("List of Products");
    g_signal_connect(window,"delete-event",G_CALLBACK(gtk_main_quit),NULL);
    g_signal_connect(button,"clicked",G_CALLBACK(button_clicked),entry);
    g_signal_connect(entry,"activate",G_CALLBACK(button_clicked),entry);
    g_signal_connect(listofprod,"clicked",G_CALLBACK(prod_list),window);
    g_signal_connect(clearb,"clicked",G_CALLBACK(clear_clicked),(gpointer)entry);
    g_signal_connect(button2,"clicked",G_CALLBACK(button2_clicked),(gpointer)entry2);
    g_signal_connect(button3,"clicked",G_CALLBACK(button3_clicked),(gpointer)window);
    g_signal_connect(entry2,"activate",G_CALLBACK(button2_clicked),(gpointer)entry2);
    gtk_table_attach(GTK_TABLE(table),button,1,2,0,1,GTK_FILL,GTK_FILL,0,0);
    gtk_table_attach(GTK_TABLE(table),entry,0,1,0,1,GTK_FILL,GTK_FILL,0,0);
    gtk_table_attach(GTK_TABLE(table),entry2,0,1,1,2,GTK_FILL,GTK_FILL,0,0);
    gtk_table_attach(GTK_TABLE(table),button2,1,2,1,2,GTK_FILL,GTK_FILL,0,0);
    gtk_table_attach(GTK_TABLE(table),clearb,2,3,0,1,GTK_FILL,GTK_FILL,0,0);
    gtk_table_attach(GTK_TABLE(table),listofprod,2,3,1,2,GTK_FILL,GTK_FILL,0,0);
    gtk_table_attach(GTK_TABLE(table),button3,0,1,2,3,GTK_FILL,GTK_FILL,0,0);
    gtk_container_add(GTK_CONTAINER(window),table);
    gtk_container_set_border_width(GTK_CONTAINER(window),100);
    gtk_widget_show_all(window);
    gtk_widget_set_size_request(window,310,310);
    gtk_main();
    return 0;
}

我已多次检查此代码。有什么问题吗?

最佳答案

你永远不能将字符串强制转换为整数。在 C 语言中,这不是“类型转换”的意思。

您必须使用实际代码进行转换。例如,查找 strtoul() 函数。

关于c - gtk_entry_get_text 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15067859/

有关c - gtk_entry_get_text 不工作的更多相关文章

  1. ruby - Facter::Util::Uptime:Module 的未定义方法 get_uptime (NoMethodError) - 2

    我正在尝试设置一个puppet节点,但ruby​​gems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由ruby​​gems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby

  2. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  3. ruby-on-rails - 'compass watch' 是如何工作的/它是如何与 rails 一起使用的 - 2

    我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t

  4. ruby - 无法让 RSpec 工作—— 'require' : cannot load such file - 2

    我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳

  5. ruby-on-rails - 使用 Sublime Text 3 突出显示 HTML 背景语法中的 ERB? - 2

    所以我在关注Railscast,我注意到在html.erb文件中,ruby代码有一个微弱的背景高亮效果,以区别于其他代码HTML文档。我知道Ryan使用TextMate。我正在使用SublimeText3。我怎样才能达到同样的效果?谢谢! 最佳答案 为SublimeText安装ERB包。假设您安装了SublimeText包管理器*,只需点击cmd+shift+P即可获得命令菜单,然后键入installpackage并选择PackageControl:InstallPackage获取包管理器菜单。在该菜单中,键入ERB并在看到包时选择

  6. ruby-on-rails - rspec should have_select ('cars' , :options => ['volvo' , 'saab' ] 不工作 - 2

    关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion在首页我有:汽车:VolvoSaabMercedesAudistatic_pages_spec.rb中的测试代码:it"shouldhavetherightselect"dovisithome_pathit{shouldhave_select('cars',:options=>['volvo','saab','mercedes','audi'])}end响应是rspec./spec/request

  7. ruby-on-rails - s3_direct_upload 在生产服务器中不工作 - 2

    在Rails4.0.2中,我使用s3_direct_upload和aws-sdkgems直接为s3存储桶上传文件。在开发环境中它工作正常,但在生产环境中它会抛出如下错误,ActionView::Template::Error(noimplicitconversionofnilintoString)在View中,create_cv_url,:id=>"s3_uploader",:key=>"cv_uploads/{unique_id}/${filename}",:key_starts_with=>"cv_uploads/",:callback_param=>"cv[direct_uplo

  8. ruby - JetBrains RubyMine 3.2.4 调试器不工作 - 2

    使用Ruby1.9.2运行IDE提示说需要gemruby​​-debug-base19x并提供安装它。但是,在尝试安装它时会显示消息Failedtoinstallgems.Followinggemswerenotinstalled:C:/ProgramFiles(x86)/JetBrains/RubyMine3.2.4/rb/gems/ruby-debug-base19x-0.11.30.pre2.gem:Errorinstallingruby-debug-base19x-0.11.30.pre2.gem:The'linecache19'nativegemrequiresinstall

  9. Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting - 2

    1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里

  10. ruby - `rescue $!` 是如何工作的? - 2

    我知道全局变量$!包含最新的异常对象,但我对下面的语法感到困惑。谁能帮助我理解以下语法?rescue$! 最佳答案 此构造可防止异常停止您的程序并使堆栈跟踪冒泡。它还会将该异常作为值返回,这很有用。a=get_me_datarescue$!在此行之后,a将保存请求的数据或异常。然后您可以分析该异常并采取相应措施。defget_me_dataraise'Nodataforyou'enda=get_me_datarescue$!puts"Executioncarrieson"pa#>>Executioncarrieson#>>#更现实的

随机推荐