PyGTK:用户登录对话框

2024-04-19 03:38:38 发布

您现在位置:Python中文网/ 问答频道 /正文

目前我正在使用python开发一个服务器客户端应用程序。 服务器客户端正在工作。我的问题是图形用户界面。 我想用GTK来做这个。 两个星期以来,我一直在测试,但登录屏幕不起作用。在

我的问题是:

  1. 如何在GTK中创建和连接多个窗口?e、 g首先登录屏幕,然后是主窗口
  2. 如何创建Text_Entry-Dialog(全部在PyGTK中)?在

[编辑-03.01.2016]代码:

#!/usr/bin/python3
# coding=utf8

import socket
from gi.repository import GObject, Gio, Gtk




class MyApplication(Gtk.Application):
    # Main initialization routine
    def __init__(self, application_id, flags):
        Gtk.Application.__init__(self, application_id=application_id, flags=flags)

        self.connect("activate", self.new_window)
        #main_Window(self)

    def new_window(self, *args):
        self. snc =start_Connection_Window(self)


        print("going on")

    def main_Window(self):
        print "Here I'm "
        self.connect("activate", self.new_MainWindow)
        self.snc.close()

    def new_MainWindow(self):

        main_Window(self)
        print "main_Window started"


class start_Connection_Window(Gtk.Window):

    def __init__(self, application):
        self.Application = application


        # Read GUI from file and retrieve objects from Gtk.Builder
        try:
            GtkBuilder = Gtk.Builder.new_from_file()#Some Glade file with two windows
            GtkBuilder.connect_signals(self)
        except GObject.GError:
            print("Error reading GUI file")
            raise

        # Fire up the main window
        self.start_Connection_Window = GtkBuilder.get_object("start_Connection")
        self.start_Connection_Window.set_application(application)


        self.ServerIP_Input = GtkBuilder.get_object("ServerIP-Input")
        self.Username_Input = GtkBuilder.get_object("Username_Input")
        self.Password_Input = GtkBuilder.get_object("Password_Input")
        self.start_Connection_Window.show()





    def  on_btn_Connect_clicked(self, button):
        button.set_sensitive(False)
        try:

            self.host = str(self.ServerIP_Input.get_text())
            self.username = str(self.Username_Input)
            self.password = str(self.Password_Input)
            self.port=1317
            self.clientsocket=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            self.clientsocket.connect((self.host, self.port))
            self.LENGTH_SIZE = 4
            data='ping'
            lenght = str(len(data))
            self.clientsocket.send(lenght.zfill(4))
            print data
            self.clientsocket.send(data)


        except:
            print "Error!"
            button.set_sensitive(True)

        self.Application.main_Window()
        try:
            lenght = self.clientsocket.recv(self.LENGTH_SIZE)
            data = self.clientsocket.recv(int(lenght))
            print data


            if(data == str("ok")):
                try:
                    self.Application.main_Window( )
                    self.close()
                except :
                    print "Here is the mistake"

            else:
                print "Failed!"
                button.set_sensitive(True)

        except:
            print "Fail!"








        #print "Would Start Conn"

    def close(self, *args):

        self.start_Connection_Window.destroy()

    def on_MainWindow_destroy(self, window):
        #self.Application.main_Window()
        print "Bye"

    def on_Window_destroy(self):

        print("Bye aus dem destroyten!")


class main_Window(Gtk.Window):

    def __init__(self, application):
        self.Application = application


        # Read GUI from file and retrieve objects from Gtk.Builder
        try:
            GtkBuilder = Gtk.Builder.new_from_file()#someGladeFile with two Windows
            GtkBuilder.connect_signals(self)
        except GObject.GError:
            print("Error reading GUI file")
            raise

        # Fire up the main window
        self.MainWindow = GtkBuilder.get_object("main_Window")
        self.MainWindow.set_application(application)
        self.MainWindow.show()



    def  on_btn_Connect_clicked(self, button):
        print "Would Start Conn"

    def close(self, *args):
        self.MainWindow.destroy()

    def on_MainWindow_destroy(self, window):
        #self.Application.new_window()
        print "Bye"

    def on_Window_destroy(self, window):
        #self.Application.new_window()
        print "Bye"

    def start(self, socket, host, username, password):
        self.SClient = SC(host, username, password, self)
        self.MainWindow.show()



# Starter
def main():
    # Initialize GTK Application
    Application = MyApplication("App", Gio.ApplicationFlags.FLAGS_NONE)

    print "starting"

    # Start GUI
    Application.run()

if __name__ == "__main__": main()

Tags: fromselfgtknewinputapplicationmaindef