使用gtk包完成页面设计实现(实战项目),带部分数据库代码。使用Ubuntu操作系统,sqlite数据库。
这里采用的是一个2:1的卡片式设计,比较美观,管理员账号密码为指定账号密码,在查询,充值,注册以及注销页面的按钮必须绑定数据库的操作。下面为几个场景的部分实例图片:
需注意,在本案例中,注销账户时并没有显示余额,应参照查询的数据库代码,将注销页面的用户余额显示在场景中,以便商家进行卡内余额退还。
#include
#include "string.h"
#include "gtk/gtk.h"
#include "sqlite3.h"
GtkWidget * window ;//定义全局
GtkWidget * window01;
GtkWidget * find_Scene;
GtkWidget * add_Scene;
GtkWidget * post_Scene;
GtkWidget * delate_Scene;
GtkWidget * label01;
GtkWidget * label_userName;
GtkWidget * entry_userName;
GtkWidget * label_passWord;
GtkWidget * entry_passWord;
GtkWidget * button01 ;
GtkWidget * label_info ;
GtkWidget * button01_01 ;
GtkWidget * button01_02 ;
GtkWidget * button01_03 ;
GtkWidget * button01_04 ;
GtkWidget * entry_addid;
GtkWidget * entry_addmoney;
GtkWidget * label_add_cur;
// GtkWidget * TEXT_ID;
GtkWidget * entry_find;
GtkWidget * label_money;
GtkWidget * entry_delate;
GtkWidget * label_del_money;
void haha(GtkButton * button,gpointer user_data)
{
printf("++++++++++\n");
const gchar * text_user = gtk_entry_get_text(GTK_ENTRY(entry_userName));
const gchar * text_pwd = gtk_entry_get_text(GTK_ENTRY(entry_passWord));
printf("%s\n",text_user);
printf("%s\n",text_pwd);
if(strcmp(text_user,"141")==0&&strcmp(text_pwd,"666")==0)
{
gtk_widget_hide_all(window);
gtk_widget_show_all(window01);
}
else{
gtk_label_set_text(GTK_LABEL(label_info),"用户名密码错误");
gtk_entry_set_text(GTK_ENTRY(entry_userName),"");
gtk_entry_set_text(GTK_ENTRY(entry_passWord),"");
}
}
void open_btn(GtkButton * button,gpointer user_data)
{
int a = (int)user_data;
if(a==0){
gtk_widget_hide_all(window01);
gtk_widget_show_all(find_Scene);
}else if(a == 1){
gtk_widget_hide_all(window01);
gtk_widget_show_all(add_Scene);
}else if(a == 2){
gtk_widget_hide_all(window01);
gtk_widget_show_all(post_Scene);
}else if(a == 3){
gtk_widget_hide_all(window01);
gtk_widget_show_all(delate_Scene);
}
}
void exit_btn(GtkButton * button,gpointer user_data)
{
int b = (int)user_data;
if(b==0){
gtk_widget_hide_all(find_Scene);
gtk_widget_show_all(window01);
}else if(b == 1){
gtk_widget_hide_all(add_Scene);
gtk_widget_show_all(window01);
}else if(b == 2){
gtk_widget_hide_all(post_Scene);
gtk_widget_show_all(window01);
}else if(b == 3){
gtk_widget_hide_all(delate_Scene);
gtk_widget_show_all(window01);
}
}
void addFun(GtkButton * button,gpointer user_data)
{
//打开数据库
sqlite3 *db;
char *errmsg=NULL;
int ret=sqlite3_open("BS.db",&db);
if(ret !=SQLITE_OK)
{
perror("sqlite3_open");
}
int c=(int)user_data;
if(c==1){
sqlite3_exec(db,"create table data_table(id int,password text);",NULL,NULL,&errmsg);
const gchar * text_password = gtk_entry_get_text(GTK_ENTRY(entry_addmoney));
const gchar * text_id = gtk_entry_get_text(GTK_ENTRY(entry_addid));
printf("注册:%s\n",text_id );
int ifPassword=strlen(text_password);
int ifId=strlen(text_id);
if(ifId==0||ifPassword==0){
gtk_label_set_markup(GTK_LABEL(label_add_cur),"非法输入");
}else{
gtk_label_set_markup(GTK_LABEL(label_add_cur),"注册成功");
char cmd[100]="";
sprintf(cmd,"insert into data_table values (%s,\'%s\');",text_id,text_password);
sqlite3_exec(db,cmd,NULL,NULL,&errmsg);
sqlite3_close(db);
}
}
}
void findFun(GtkButton * button,gpointer user_data)
{
sqlite3 *db;
char *errmsg=NULL;
const gchar * find_id = gtk_entry_get_text(GTK_ENTRY(entry_find));
int ret=sqlite3_open("BS.db",&db);
if(ret !=SQLITE_OK)
{
perror("sqlite3_open");
}
char **table=NULL;
int r=0,c=0;
char cmds[100]="";
sprintf(cmds,"select *from data_table where id=%s;",find_id);
printf("%s\n",find_id);
sqlite3_get_table(db,cmds,&table,&r,&c,&errmsg);
// int i,j;
printf("%s ",table[3]);
// int t = ("%s\n",table[3]);
gtk_label_set_markup(GTK_LABEL(label_money),("%s\n",table[3]));
sqlite3_close(db);
}
void postFun(GtkButton * button,gpointer user_data)//需修改
{
sqlite3 *db;
char *errmsg=NULL;
char ** table =NULL;
int r=0,c=0;
char cmd2[100]="";
sprintf(cmd2,"select * from date_table;");
sqlite3_get_table(db,cmd2,&table,&r,&c,&errmsg);
int i,j;
for (i = 0; i < r+1; ++i)
{
for (j = 0; j < c ; ++j)
{
printf("%s ",table[i*c+j]);
}
printf("\n");
}
}
void delateFun(GtkButton * button,gpointer user_data)
{
sqlite3 *db;
char *errmsg=NULL;
const gchar * delate_id = gtk_entry_get_text(GTK_ENTRY(entry_delate));
int ret=sqlite3_open("BS.db",&db);
if(ret !=SQLITE_OK)
{
perror("sqlite3_open");
}
char **table=NULL;
int r=0,c=0;
char cmd2[100]="";
sprintf(cmd2,"delete from data_table where id=%s;",delate_id);
printf("%s\n",delate_id);
sqlite3_exec(db,cmd2,NULL,NULL,&errmsg);
gtk_label_set_markup(GTK_LABEL(label_del_money),"注销成功");
sqlite3_close(db);
}
void load_image(GtkWidget *image, const char *file_path, const int w, const int h )
{
gtk_image_clear( GTK_IMAGE(image) ); // 清除图像
GdkPixbuf *src_pixbuf = gdk_pixbuf_new_from_file(file_path, NULL); // 创建图片资源
GdkPixbuf *dest_pixbuf=gdk_pixbuf_scale_simple(src_pixbuf,w,h,GDK_INTERP_BILINEAR); // 指定大小
gtk_image_set_from_pixbuf(GTK_IMAGE(image), dest_pixbuf); // 图片控件重新设置一张图片(pixbuf)
g_object_unref(src_pixbuf); // 释放资源
g_object_unref(dest_pixbuf); // 释放资源
}
int main(int argc, char *argv[])
{
gtk_init(&argc,&argv);
//第一个页面,window
window =gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_widget_set_size_request(window,1200,600);
GtkWidget *fixed = gtk_fixed_new();
gtk_container_add(GTK_CONTAINER(window),fixed);
//bg,纯色背景图片
GtkWidget * image = gtk_image_new_from_pixbuf(NULL);
load_image(image,"./img/color.jpg",1200,600);
gtk_fixed_put(GTK_FIXED(fixed),image,0,0);//固定布局
//左上图片
GtkWidget * image01 = gtk_image_new_from_pixbuf(NULL);
load_image(image01,"./img/1000144.jpg",800,600);//
gtk_fixed_put(GTK_FIXED(fixed),image01,0,0);//固定布局
//logo,名称
label01=gtk_label_new("");
gtk_label_set_markup(GTK_LABEL(label01),"胜游网络");
gtk_fixed_put(GTK_FIXED(fixed),label01,110,60);
GtkWidget * label_tips=gtk_label_new("");
gtk_label_set_markup(GTK_LABEL(label_tips),"下午好,欢迎光临胜游网络");
gtk_fixed_put(GTK_FIXED(fixed),label_tips,600,580);
GtkWidget * label_admin=gtk_label_new("");
gtk_label_set_markup(GTK_LABEL(label_admin),"管理员请登录");
gtk_fixed_put(GTK_FIXED(fixed),label_admin,900,200);
//管理员账号密码
label_userName=gtk_label_new("");
gtk_label_set_markup(GTK_LABEL(label_userName),"账号");
gtk_fixed_put(GTK_FIXED(fixed),label_userName,880,260);
entry_userName=gtk_entry_new();
gtk_fixed_put(GTK_FIXED(fixed),entry_userName,930,260);
gtk_entry_set_max_length(GTK_ENTRY(entry_userName),12);
label_passWord=gtk_label_new("");
gtk_label_set_markup(GTK_LABEL(label_passWord),"密码");
gtk_fixed_put(GTK_FIXED(fixed),label_passWord,880,300);
entry_passWord=gtk_entry_new();
gtk_fixed_put(GTK_FIXED(fixed),entry_passWord,930,300);
gtk_entry_set_max_length(GTK_ENTRY(entry_passWord),20);
gtk_entry_set_visibility(GTK_ENTRY(entry_passWord),FALSE);
//输入错误
label_info=gtk_label_new("");
gtk_label_set_markup(GTK_LABEL(label_info),"");
gtk_fixed_put(GTK_FIXED(fixed),label_info,930,330);
//按钮
button01 = gtk_button_new_with_label("登录");
// GtkWidget * image01 = gtk_image_new_from_pixbuf(NULL);
// load_image(image01,"./img/bg.jpg",100,100);
// gtk_button_set_image(GTK_BUTTON(button01),image01);
gtk_fixed_put(GTK_FIXED(fixed),button01,1050,350);
g_signal_connect(button01,"pressed",G_CALLBACK(haha),NULL);
//进入管理员场景
window01 =gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_widget_set_size_request(window01,1000,562);
GtkWidget *fixed01 = gtk_fixed_new();
gtk_container_add(GTK_CONTAINER(window01),fixed01);
//菜单场景
GtkWidget * image01_01 = gtk_image_new_from_pixbuf(NULL);
load_image(image01_01,"./img/1001639.jpg",1000,562);
gtk_fixed_put(GTK_FIXED(fixed01),image01_01,0,0);//固定布局
//菜单场景按钮
button01_01 = gtk_button_new_with_label("查询余额");
gtk_fixed_put(GTK_FIXED(fixed01),button01_01,450,150);
g_signal_connect(button01_01,"pressed",G_CALLBACK(open_btn),(gpointer)0);
button01_02 = gtk_button_new_with_label("会员充值");
gtk_fixed_put(GTK_FIXED(fixed01),button01_02,450,210);
g_signal_connect(button01_02,"pressed",G_CALLBACK(open_btn),(gpointer)2);
button01_03 = gtk_button_new_with_label("会员注册");
gtk_fixed_put(GTK_FIXED(fixed01),button01_03,450,270);
g_signal_connect(button01_03,"pressed",G_CALLBACK(open_btn),(gpointer)1);
button01_04 = gtk_button_new_with_label("注销会员");
gtk_fixed_put(GTK_FIXED(fixed01),button01_04,450,330);
g_signal_connect(button01_04,"pressed",G_CALLBACK(open_btn),(gpointer)3);
//find_场景
find_Scene =gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_widget_set_size_request(find_Scene,1000,562);
GtkWidget *fixed_find = gtk_fixed_new();
gtk_container_add(GTK_CONTAINER(find_Scene),fixed_find);
GtkWidget * image_find = gtk_image_new_from_pixbuf(NULL);
load_image(image_find,"./img/1001639.jpg",1000,562);
gtk_fixed_put(GTK_FIXED(fixed_find),image_find,0,0);//固定布局
//返回按钮
GtkWidget * exit_find = gtk_button_new_with_label("返回");
gtk_fixed_put(GTK_FIXED(fixed_find),exit_find,0,0);
g_signal_connect(exit_find,"pressed",G_CALLBACK(exit_btn),(gpointer)0);
GtkWidget * label_findLaebl=gtk_label_new("");
gtk_label_set_markup(GTK_LABEL(label_findLaebl),"查询余额");
gtk_fixed_put(GTK_FIXED(fixed_find),label_findLaebl,420,100);
GtkWidget * label_find=gtk_label_new("");
gtk_label_set_markup(GTK_LABEL(label_find),"账号");
gtk_fixed_put(GTK_FIXED(fixed_find),label_find,380,220);
entry_find=gtk_entry_new();
gtk_fixed_put(GTK_FIXED(fixed_find),entry_find,430,220);
gtk_entry_set_max_length(GTK_ENTRY(entry_find),12);
//余额显示
label_money=gtk_label_new("");
gtk_label_set_markup(GTK_LABEL(label_money),"");
gtk_fixed_put(GTK_FIXED(fixed_find),label_money,430,260);
GtkWidget * find_btn = gtk_button_new_with_label("查询余额");
gtk_fixed_put(GTK_FIXED(fixed_find),find_btn,520,300);
g_signal_connect(find_btn,"pressed",G_CALLBACK(findFun),NULL);//btn
//add场景添加会员
add_Scene =gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_widget_set_size_request(add_Scene,1000,562);
GtkWidget *fixed_add = gtk_fixed_new();
gtk_container_add(GTK_CONTAINER(add_Scene),fixed_add);
GtkWidget * image_add = gtk_image_new_from_pixbuf(NULL);
load_image(image_add,"./img/1001639.jpg",1000,562);
gtk_fixed_put(GTK_FIXED(fixed_add),image_add,0,0);//固定布局
//返回按钮
GtkWidget * exit_add = gtk_button_new_with_label("返回");
gtk_fixed_put(GTK_FIXED(fixed_add),exit_add,0,0);
g_signal_connect(exit_add,"pressed",G_CALLBACK(exit_btn),(gpointer)1);
GtkWidget * label_addLabel=gtk_label_new("");
gtk_label_set_markup(GTK_LABEL(label_addLabel),"会员注册");
gtk_fixed_put(GTK_FIXED(fixed_add),label_addLabel,420,100);
GtkWidget * label_addid=gtk_label_new("");
gtk_label_set_markup(GTK_LABEL(label_addid),"账号");
gtk_fixed_put(GTK_FIXED(fixed_add),label_addid,380,220);
entry_addid=gtk_entry_new();
gtk_fixed_put(GTK_FIXED(fixed_add),entry_addid,430,220);
gtk_entry_set_max_length(GTK_ENTRY(entry_addid),12);
GtkWidget * label_addmoney=gtk_label_new("");
gtk_label_set_markup(GTK_LABEL(label_addmoney),"金额");
gtk_fixed_put(GTK_FIXED(fixed_add),label_addmoney,380,250);
entry_addmoney=gtk_entry_new();
gtk_fixed_put(GTK_FIXED(fixed_add),entry_addmoney,430,250);
gtk_entry_set_max_length(GTK_ENTRY(entry_addmoney),20);
//注册成功
label_add_cur=gtk_label_new("");
gtk_label_set_markup(GTK_LABEL(label_add_cur),"");
gtk_fixed_put(GTK_FIXED(fixed_add),label_add_cur,430,280);
GtkWidget * add_btn = gtk_button_new_with_label("确认");
gtk_fixed_put(GTK_FIXED(fixed_add),add_btn,520,320);
g_signal_connect(add_btn,"pressed",G_CALLBACK(addFun),(gpointer)1);//btn
//post场景
post_Scene =gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_widget_set_size_request(post_Scene,1000,562);
GtkWidget *fixed_post = gtk_fixed_new();
gtk_container_add(GTK_CONTAINER(post_Scene),fixed_post);
GtkWidget * image_post = gtk_image_new_from_pixbuf(NULL);
load_image(image_post,"./img/1001639.jpg",1000,562);
gtk_fixed_put(GTK_FIXED(fixed_post),image_post,0,0);//固定布局
//返回按钮
GtkWidget * exit_post = gtk_button_new_with_label("返回");
gtk_fixed_put(GTK_FIXED(fixed_post),exit_post,0,0);
g_signal_connect(exit_post,"pressed",G_CALLBACK(exit_btn),(gpointer)2);
GtkWidget * label_postLabel=gtk_label_new("");
gtk_label_set_markup(GTK_LABEL(label_postLabel),"充值");
gtk_fixed_put(GTK_FIXED(fixed_post),label_postLabel,420,100);
GtkWidget * label_postid=gtk_label_new("");
gtk_label_set_markup(GTK_LABEL(label_postid),"账号");
gtk_fixed_put(GTK_FIXED(fixed_post),label_postid,380,220);
GtkWidget * entry_postid=gtk_entry_new();
gtk_fixed_put(GTK_FIXED(fixed_post),entry_postid,430,220);
gtk_entry_set_max_length(GTK_ENTRY(entry_postid),12);
GtkWidget * label_postmoney=gtk_label_new("");
gtk_label_set_markup(GTK_LABEL(label_postmoney),"金额");
gtk_fixed_put(GTK_FIXED(fixed_post),label_postmoney,380,250);
GtkWidget * entry_postmoney=gtk_entry_new();
gtk_fixed_put(GTK_FIXED(fixed_post),entry_postmoney,430,250);
gtk_entry_set_max_length(GTK_ENTRY(entry_postmoney),20);
//添加成功
GtkWidget * label_post_cur=gtk_label_new("");
gtk_label_set_markup(GTK_LABEL(label_post_cur),"");
gtk_fixed_put(GTK_FIXED(fixed_post),label_post_cur,430,280);
GtkWidget * post_btn = gtk_button_new_with_label("确认充值");
gtk_fixed_put(GTK_FIXED(fixed_post),post_btn,520,320);
g_signal_connect(post_btn,"pressed",G_CALLBACK(postFun),NULL);//btn
//delate场景
delate_Scene =gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_widget_set_size_request(delate_Scene,1000,562);
GtkWidget *fixed_delate = gtk_fixed_new();
gtk_container_add(GTK_CONTAINER(delate_Scene),fixed_delate);
GtkWidget * image_delate = gtk_image_new_from_pixbuf(NULL);
load_image(image_delate,"./img/1001639.jpg",1000,562);
gtk_fixed_put(GTK_FIXED(fixed_delate),image_delate,0,0);//固定布局
//返回按钮
GtkWidget * exit_delate = gtk_button_new_with_label("返回");
gtk_fixed_put(GTK_FIXED(fixed_delate),exit_delate,0,0);
g_signal_connect(exit_delate,"pressed",G_CALLBACK(exit_btn),(gpointer)3);
GtkWidget * label_delateLaebl=gtk_label_new("");
gtk_label_set_markup(GTK_LABEL(label_delateLaebl),"注销账户");
gtk_fixed_put(GTK_FIXED(fixed_delate),label_delateLaebl,420,100);
GtkWidget * label_delate=gtk_label_new("");
gtk_label_set_markup(GTK_LABEL(label_delate),"账号");
gtk_fixed_put(GTK_FIXED(fixed_delate),label_delate,380,220);
entry_delate=gtk_entry_new();
gtk_fixed_put(GTK_FIXED(fixed_delate),entry_delate,430,220);
gtk_entry_set_max_length(GTK_ENTRY(entry_delate),12);
//余额显示,退还余额
label_del_money=gtk_label_new("");
gtk_label_set_markup(GTK_LABEL(label_del_money),"");
gtk_fixed_put(GTK_FIXED(fixed_delate),label_del_money,430,260);
GtkWidget * delate_btn = gtk_button_new_with_label("确认注销");
gtk_fixed_put(GTK_FIXED(fixed_delate),delate_btn,520,300);
g_signal_connect(delate_btn,"pressed",G_CALLBACK(delateFun),NULL);//btn
gtk_widget_show_all(window);
// gtk_widget_show_all(delate_Scene);
gtk_main();
return 0;
}