Ad

Two Instances Of Tkinter Window Created When Run Python Script As Administrator

- 1 answer

I am developing small app using tkinter UI which has a windows and a small form with three input and a button.

It requires to run with Administrator privileges (some file operation in C:\). I use following code and it works fine.

import admin
if not admin.isUserAdmin():
    admin.runAsAdmin()

Only problem I face is when I run python script(.py), it creates two instances of same window. Even two Python environment windows(black cmd screen of python.exe) appear.

  1. UAC dialog appears to ask for permission to run as admin. When I press "Yes", window appears as admin.
  2. Simple window (same app) but without admin privileges. Appears after closing of 1st window.

I am using Windows 10 and Visual Studio 2015 Pro and Python 2.7.

This is how I initialize UI.

window = Tk()
window.title("User Interface")
.........
.........
# all other code blocks(UI + Business Logic) appear here.
.........
.........
window.mainloop()
Ad

Answer

Just use:

import admin
if not admin.isUserAdmin():
    admin.runAsAdmin()
    quit()

As I understand, you run the same program (admin.runAsAdmin()), but you don't close the non-admin one. Use quit() or sys.exit() for that. Hope that's helpful!

Ad
source: stackoverflow.com
Ad