How can I make running a command depend on the input of a correct password (Tkinter GUI)?

Ashwin Joshi

I created a small GUI in Tkinter/python:

import Tkinter
import tkMessageBox
import os
top = Tkinter.Tk()
top.geometry("400x250")

def root_login():
    os.system("gksudo su && sudo su")   
    tkMessageBox.showinfo("Login successful!")

def close_window():
    top.destroy()


B = Tkinter.Button(top, text ="Login", command = root_login)
B.pack()

Q = Tkinter.Button(top, text ="Quit", command = close_window)
Q.pack()

top.mainloop()

If an incorrect password is given in the gksudo su dialog, the dialogue still shows "Login successful!".

How do I show that the password input was wrong, instead of "Login successful!". I want to create this window as a login screen for the application I'm building.

Jacob Vlijm

On the edge of being off-topic, but for the sake of gksudo:

Not sure what you want to achieve, since the GUI does not have any effect on what happens in the terminal :)

Then technically

The problem is that you do not set a condition for tkMessageBox.showinfo("Login successful!") to be executed, so whatever happens in os.system("gksudo su && sudo su"), the next line will be performed.

def root_login():
    os.system("gksudo su && sudo su")   
    tkMessageBox.showinfo("Login successful!")

How to make it work

First, you shouldn't use os.system any more: Really, really old fashioned.

See below for an alternative coding, using subprocess.check_call:

#!/usr/bin/env python
import subprocess
import Tkinter
import tkMessageBox

top = Tkinter.Tk()
top.geometry("400x250")

def root_login():
    try:
        subprocess.check_call(["gksudo", "su"])
    except subprocess.CalledProcessError:
        tkMessageBox.showinfo("message", "OOOOPS...\nWrong password!")
    else:
        tkMessageBox.showinfo("message", "Login successful!")

def close_window():
    top.destroy()

B = Tkinter.Button(top, text ="Login", command = root_login)
B.pack()
Q = Tkinter.Button(top, text ="Quit", command = close_window)
Q.pack()

top.mainloop()

But again, gksudo su does not have any effect, since you run a GUI :)

Explanation

subprocess.check_call(["gksudo", "su"])

...will raise a subprocess.CalledProcessError in case the password is incorrect, and show the message:

enter image description here

If the password is correct, the message:

enter image description here

will appear.

Note

The try/except/else construction, I did for clarity reasons. The function below does exactly the same, since the function wil "jump" from the line:

subprocess.check_call(["gksudo", "su"])

to

tkMessageBox.showinfo("message", "OOOOPS...\nWrong password!")

in case, and immediately if, the first one raises the subprocess.CalledProcessError:

def root_login():
    try:
        subprocess.check_call(["gksudo", "su"])
        tkMessageBox.showinfo("message", "Login successful!")
    except subprocess.CalledProcessError:
        tkMessageBox.showinfo("message", "OOOOPS...\nWrong password!")

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

How do I run a sudo command needing password input in the background?

분류에서Dev

how can i make a username and password test method that doesnt overlap?

분류에서Dev

How to make sure that the process is still running so that I can kill it?

분류에서Dev

How can i make ghostsctip command in python work?

분류에서Dev

How can I make sure that I'm running an updated version of bash?

분류에서Dev

How can I execute more a command based on the Button that called it? (Classes and tkinter)

분류에서Dev

How can I make audio input work with Intel NUC5i3RYK

분류에서Dev

How can I make a connection with a device, on which an API is running, through IP?

분류에서Dev

How can I make a connection with a device, on which an API is running, through IP?

분류에서Dev

How to make the top bar of my terminal say what command is running?

분류에서Dev

How do I determine the correct commend for running a PHP CLI script?

분류에서Dev

Can I see the summed up results of the ping command while it is running?

분류에서Dev

How do I boot to a command line interface instead of a GUI?

분류에서Dev

How do I run a gui app from the command line?

분류에서Dev

How to make GUI in DOS?

분류에서Dev

How can I get a Nexus 4 running 15.04 OTA-12 to connect to a particular wi-fi network when it won't accept the password

분류에서Dev

How should I proceed after running the collect command?

분류에서Dev

How do I get a list of running applications by using the command line?

분류에서Dev

How do I give an input to an infinitely running program?

분류에서Dev

How can I save the last command to a file?

분류에서Dev

How can I get the source code of a command?

분류에서Dev

How can I make an element appear (for instance a tick) when a certain amount of characters have been typed into an input field?

분류에서Dev

How can i change the color of my lines in drawing GUI

분류에서Dev

Can I see in a log file all GUI based tasks in its alternative command-line format?

분류에서Dev

How can I make IBus not ignore ~/.XCompose?

분류에서Dev

How can i make this css star smaller?

분류에서Dev

How can I make export permanent?

분류에서Dev

How can I make a functioning animated button?

분류에서Dev

How can I make this JButton work

Related 관련 기사

  1. 1

    How do I run a sudo command needing password input in the background?

  2. 2

    how can i make a username and password test method that doesnt overlap?

  3. 3

    How to make sure that the process is still running so that I can kill it?

  4. 4

    How can i make ghostsctip command in python work?

  5. 5

    How can I make sure that I'm running an updated version of bash?

  6. 6

    How can I execute more a command based on the Button that called it? (Classes and tkinter)

  7. 7

    How can I make audio input work with Intel NUC5i3RYK

  8. 8

    How can I make a connection with a device, on which an API is running, through IP?

  9. 9

    How can I make a connection with a device, on which an API is running, through IP?

  10. 10

    How to make the top bar of my terminal say what command is running?

  11. 11

    How do I determine the correct commend for running a PHP CLI script?

  12. 12

    Can I see the summed up results of the ping command while it is running?

  13. 13

    How do I boot to a command line interface instead of a GUI?

  14. 14

    How do I run a gui app from the command line?

  15. 15

    How to make GUI in DOS?

  16. 16

    How can I get a Nexus 4 running 15.04 OTA-12 to connect to a particular wi-fi network when it won't accept the password

  17. 17

    How should I proceed after running the collect command?

  18. 18

    How do I get a list of running applications by using the command line?

  19. 19

    How do I give an input to an infinitely running program?

  20. 20

    How can I save the last command to a file?

  21. 21

    How can I get the source code of a command?

  22. 22

    How can I make an element appear (for instance a tick) when a certain amount of characters have been typed into an input field?

  23. 23

    How can i change the color of my lines in drawing GUI

  24. 24

    Can I see in a log file all GUI based tasks in its alternative command-line format?

  25. 25

    How can I make IBus not ignore ~/.XCompose?

  26. 26

    How can i make this css star smaller?

  27. 27

    How can I make export permanent?

  28. 28

    How can I make a functioning animated button?

  29. 29

    How can I make this JButton work

뜨겁다태그

보관