Ik heb een python programma shutdown met keuzemogelijkheden shutdown, reboot en logout en een countdowntimer.
Ik gebruik tkinter. In de bijlage is een voorbeeld van het window met daarin de drie keuzemogelijkheden, direct na het starten van de programmaatje.
De eerste optie (shutdown) is hier geselecteerd maar niet gehighlight. Met de pijltjestoetsen kan ik de andere opties selecteren maar die worden ook niet gehighlight.
Als ik met de muis op één van de opties klik dan wordt deze geselecteerd en gehighlight. Daarna kan ik zowel met de muis als met de pijltjestoetsen een andere optie kiezen waarbij deze zowel geselecteerd wordt als gehighlight.
Hoe kan ik mijn programmaa aanpassen zodat bij start de eerste optie geselecteerd wordt èn gehighlight?
Hier is het programma:
#!/usr/bin/env python3
import subprocess
import tkinter as tk
import os
import re
# constants
MAXTIME=30
# global variables
selection = 0
isOk = False
timer = MAXTIME
# get screenresolution
output = subprocess.Popen('xrandr | grep "\*" | cut -d" " -f4',shell=True, stdout=subprocess.PIPE).communicate()[0] #get resolutions
output = re.split(r'[^\w]', str(output)) #remove special characters
output=output[1] #get first resolution
output=output.split('x') # split in [x,y]
maxX=int(output[0])
maxY=int(output[1])
# set workdirectory
os.chdir(os.path.dirname(__file__))
# show the shutdown selection window
root = tk.Tk()
root.title("Shutdown")
root.iconphoto(False, tk.PhotoImage(file='shutdown.png'))
Tk_Width = 400
Tk_Height = 200
x_Left = int(maxX/2 - Tk_Width/2)
y_Top = int(maxY/2 - Tk_Height/2)
root.geometry("+{}+{}".format(x_Left, y_Top))
root.resizable(False, False)
# set timeText variable for showing countdown
timeText = tk.StringVar()
timeText.set(str(timer))
# countdown timer
def countdown():
global timer
if timer == 0:
okAction(0)
timer = timer - 1
timeText.set(str(timer))
root.after(1000,countdown)
# reset timer after selection
def setSelected(event):
global timer
global selection
timer = MAXTIME
timeText.set(str(timer))
selected_indices = List.curselection()
try:
selection = selected_indices[0]
except:
selection = 0
# cancelButton action
def cancelAction(event):
global isOk
isOk = False
root.quit()
# okButton action
def okAction(event):
global selection
global isOk
isOk = True
selected_indices = List.curselection()
selection = selected_indices[0]
root.quit()
#Up action
def upAction(event):
global timer
global selection
timer = MAXTIME
timeText.set(str(timer))
selected_indices = List.curselection()
index = selected_indices[0]
List.select_clear(index)
if index > 0:
index = index - 1
List.select_clear(0, 2)
List.selection_set(index)
List.see(index)
List.activate(index)
selected_indices = List.curselection()
selection = selected_indices[0]
#Down action
def downAction(event):
global timer
global selection
timer = MAXTIME
timeText.set(str(timer))
selected_indices = List.curselection()
index = selected_indices[0]
List.select_clear(index)
if index < 2:
index = index + 1
List.selection_set(index)
List.see(index)
List.activate(index)
selected_indices = List.curselection()
selection = selected_indices[0]
List_border = tk.Frame(root, bd=1, relief="sunken", background="#EFF0F1")
List = tk.Listbox(List_border, font=("Arial", 14), width=25, height=3, selectmode=tk.SINGLE, borderwidth=0, highlightthickness=0 )
List.insert(1, " Shutdown")
List.insert(2, " Reboot")
List.insert(3, " Logout")
List.selection_set(0)
List.bind('<<ListboxSelect>>', setSelected)
List.bind('<Up>', upAction)
List.bind('<Down>', downAction)
List.bind('<Return>', okAction)
List.bind('<KP_Enter>', okAction)
List.bind('<Escape>', cancelAction)
cancelButton = tk.Button(root, text="Cancel", font=("Arial", 14), height = 1, width = 6, command=lambda: cancelAction(0))
timeLabel = tk.Label(root, textvariable = timeText, font=("Arial", 14), height = 1, width = 6)
okButton = tk.Button(root, text="OK", font=("Arial", 14), height = 1, width = 6, command=lambda: okAction(0))
List_border.grid(row=0,column=0,columnspan=3)
List.pack(padx=10, pady=20, fill="both", expand=True)
cancelButton.grid(row=1,column=0)
timeLabel.grid(row=1,column=1)
okButton.grid(row=1,column=2)
List.focus()
root.after(1000,countdown)
root.mainloop()
# the actual shutdown, reboot and logout commands
if isOk == True:
if selection == 0:
# poweroff shutdown
bashCommand = "/usr/bin/systemctl poweroff"
elif selection == 1:
# poweroff reboot
bashCommand = "/usr/bin/systemctl reboot"
elif selection == 2:
# logout
userName = subprocess.run(["whoami"],stdout=subprocess.PIPE, text=True)
userName = userName.stdout
bashCommand = "pkill -KILL -u " + userName
subprocess.run(bashCommand.split())