jeudi 27 août 2015

IF statement using Python 3 tkinter

What I would like to do is create a password mechanism for a GUI I have previously created. This is what I have so far:

   import tkinter as tk
   from tkinter import*
   import ctypes

   def runUnturned():
       ctypes.windll.shell32.ShellExecuteW(0, 'open', 'C:\\Program Files (x86)\\Steam\\steamapps\\common\\Unturned\\UnturnedServer', None, None, 10)

   def RUNCMD():
       ctypes.windll.shell32.ShellExecuteW(0, 'open', 'cmd.exe', '/k ipconfig', None, 10)

   class PasswordDialog(tk.Toplevel):
         def __init__(self, parent):
             tk.Toplevel.__init__(self)
             self.parent = parent
             self.entry = tk.Entry(self, show='*')
             self.entry.bind("<KeyRelease-Return>", self.StorePassEvent)
             self.entry.pack()
             self.button = tk.Button(self)
             self.button["text"] = "Submit"
             self.button["command"] = self.StorePass
             self.button.pack()

def StorePassEvent(self, event):
    self.StorePass()

def StorePass(self):
    self.parent.password = self.entry.get()
    self.destroy()

class UsernameDialog(tk.Toplevel):
     def __init__(self, parent):
         tk.Toplevel.__init__(self)
         self.parent = parent
         self.entry = tk.Entry(self)
         self.entry.bind("<KeyRelease-Return>", self.StorePassEvent)
         self.entry.pack()
         self.button = tk.Button(self)
         self.button["text"] = "Submit"
         self.button["command"] = self.StorePass
         self.button.pack()

    def StorePassEvent(self, event):
        self.StorePass()

   def StorePass(self):
       self.parent.username = self.entry.get()
       self.destroy()

class Application1(tk.Frame):
     def __init__(self, root):
        tk.Frame.__init__(self, root)
        self.password = None
        self.username = None
        self.button = tk.Button(self)
        self.button1 = tk.Button(self)
        self.button1["text"] = "Username"
        self.button1["command"] = self.GetUsername
        self.button1.pack()
        self.button = tk.Button(self)
        self.button["text"] = "Password"
        self.button["command"] = self.GetPassword
        self.button.pack()

   def GetPassword(self):
       self.wait_window(PasswordDialog(self))
       print("Password: ", self.password)

   def GetUsername(self):
       self.wait_window(UsernameDialog(self))
       print("Username: ", self.username)

class MainApplication(tk.Frame):
      def __init__(self, master):
          super(Application, self).__init__(master)
          self.grid()
          self.create_widgets()

      def create_widgets(self):
          btnb = Button(app, text = "Run CMD", command=RUNCMD)
          btnb.grid()
          btna = Button(app, text = 'Unturned Server', command=runUnturned)
          btna.grid()

if __name__ == "__main__":
   root = tk.Tk()
   root.geometry("200x100")
   Application1(root).pack(side="top", fill="both", expand=True)
   root.mainloop()

if ??? == "excelsior":
   root = tk.Tk()
   root.title("My Servers")
   root.geometry("400x400")
   MainApplication(root).pack(side="top", fill="both", expand=True)
   root.mainloop()

Notice that the final if statement has a "???" instead of "password". That is because I am unsure of what to put there and there is no predefined class or variable called password. Can someone help me figure out what I have done?

Aucun commentaire:

Enregistrer un commentaire