![如何使用 gtk3 c++ 代码设计登录页面?](https://linux22.com/image/1140703/%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8%20gtk3%20c%2B%2B%20%E4%BB%A3%E7%A0%81%E8%AE%BE%E8%AE%A1%E7%99%BB%E5%BD%95%E9%A1%B5%E9%9D%A2%EF%BC%9F.png)
我想用 gtk3 c++ 代码设计登录页面。这是我的 c++ 代码:
#include <gtk/gtk.h>
void create_window(GtkWidget *button, gpointer window) {
GtkWidget *win, *label;
//Username and Password to validate credentials
const string USERNAME = "user";
const string PASSWORD = "123456";
string username, password;
//Checking if user's entered credentials are equal to actual USERNAME and PASSWORD
if (username == USERNAME && password == PASSWORD)
{
label = gtk_label_new("Username and Password is correct.");
win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_container_add(GTK_CONTAINER(win), label);
gtk_widget_show_all(win);
}
else if (username != USERNAME || password != PASSWORD)
{
label = gtk_label_new("Username and Password is incorrect.");
win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_container_add(GTK_CONTAINER(win), label);
gtk_widget_show_all(win);
}
}
static void destroy(GtkWidget *widget, gpointer data){
gtk_main_quit();
}
int main(int argc, char *argv[]){
GtkWidget *window;
GtkWidget *grid;
GtkWidget *Login_button, *Quit_button;
GtkWidget *u_name;
GtkWidget *pass;
GtkWidget *label_user;
GtkWidget *label_pass;
GtkWidget *button_container;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "Login page");
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_container_set_border_width(GTK_CONTAINER(window), 10);
gtk_window_set_resizable(GTK_WINDOW(window), FALSE);
grid = gtk_grid_new();
gtk_grid_set_row_spacing(GTK_GRID(grid), 3);
gtk_container_add(GTK_CONTAINER(window), grid);
label_user = gtk_label_new("Username ");
label_pass = gtk_label_new("Password ");
u_name = gtk_entry_new();
gtk_entry_set_placeholder_text(GTK_ENTRY(u_name), "Username");
gtk_grid_attach(GTK_GRID(grid), label_user, 0, 1, 1, 1);
gtk_grid_attach(GTK_GRID(grid), u_name, 1, 1, 2, 1);
pass = gtk_entry_new();
gtk_entry_set_placeholder_text(GTK_ENTRY(pass), "Password");
gtk_grid_attach(GTK_GRID(grid), label_pass, 0, 2, 1, 1);
gtk_entry_set_visibility(GTK_ENTRY(pass), 0);
gtk_grid_attach(GTK_GRID(grid), pass, 1, 2, 1, 1);
Login_button = gtk_button_new_with_label("Log in");
g_signal_connect(Login_button, "clicked", G_CALLBACK(create_window), NULL);
gtk_grid_attach(GTK_GRID(grid), Login_button, 0, 3, 2, 1);
Quit_button = gtk_button_new_with_label("Quit");
g_signal_connect(Quit_button, "clicked", G_CALLBACK(gtk_main_quit), NULL);
gtk_grid_attach(GTK_GRID(grid), Quit_button, 0, 4, 2, 1);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
但我无法将用户名和密码引入我的代码中。
例如,用户名是 Alireza,密码是 @12345@。
当我输入用户名和密码时,如果用户名和密码正确,登录页面将被销毁,第二个页面(新页面)将打开。但如果其中任何一个不正确,我们应该在新页面上显示以下消息:
如果用户名不正确,则消息为:用户名不正确如果密码不正确,则消息为:密码不正确。
答案1
我不知道您是否正在使用数据库,但如果没有,您需要创建一个函数,如您在信号“create_window”中命名的函数,(g_signal_connect(Login_button, "clicked", G_CALLBACK(create_window), NULL);)
在此函数中添加您的检查代码
If (Username and Password was correct)
Login page destroy and seconde page (new page) open
If (Username was correct)
Username is incorrect
If (Password was incorrect)
Password is incorrect.
如果这对你不起作用,请重播,我将使用 gtkmm/c++ 编写正确的代码
这是函数,但你需要编写消息
static void create_window (GtkWidget *widget, gpointer data) { if( USERNAME != gtk_entry_get_text (GtkEntry *u_name) ) { //您的消息在这里输出 //g_print ("用户名不正确。\n"); } if( PASSWORD != gtk_entry_get_text (GtkEntry *pass) ) { //您的消息在这里输出 //g_print ("密码不正确。\n"); }
}