Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Run tkinter code in another thread
- from tkinter import *
- from tkinter import messagebox, messagebox
- from tkinter.ttk import Separator
- from tracemalloc import start
- from pystray import MenuItem as item
- import pystray
- from PIL import ImageTk,Image
- import screen_brightness_control as sbc
- import win32api
- import win32con
- import pywintypes
- import psutil
- import time
- import threading
- import pickle
- import webbrowser
- import sys
- import os
- os.chdir(sys.path[0])
- class App(threading.Thread):
- def __init__(self):
- threading.Thread.__init__(self)
- self.start()
- def callback(self):
- self.root.quit()
- def run(self):
- # Define a function for quit the window
- def popupInfo():
- messagebox.showinfo("ITExtra Readme", "App use case: This app was mainly developed to save battery on mobile devices.\n\n1. When you plug in your charger, it automatically sets it to the maximum refresh rate specified.\n\n2. When you pull out your charger, it automatically lowers it to the minimum specified.\n\nThus saving some battery life.\n\nHow to use?\nYou can set your minimum and maximum refresh rate in the GUI.\n\nProgram created by ITextra")
- # Creates popup error window
- def popupError():
- messagebox.showerror("ITExtra RR Change", "Error.")
- def hidden(root):
- global my_img1
- top=Toplevel(root)
- top.title("Secret menu, shhh :^)")
- top.overrideredirect(True)
- top.attributes('-alpha', 0.9)
- # Set root geometry size
- w = 1100 # width for the Tk root
- h = 450 # height for the Tk root
- # get screen width and height
- ws = top.winfo_screenwidth() # width of the screen
- hs = top.winfo_screenheight() # height of the screen
- # calculate x and y coordinates for the Tk root window
- x = (ws/2) - (w/2)
- y = (hs/3) - (h/2)
- # set the dimensions of the screen
- # and where it is placed
- top.geometry('%dx%d+%d+%d' % (w, h, x, y))
- top.iconbitmap('screen.ico')
- my_img1 = ImageTk.PhotoImage(Image.open("ITEXTRA.png"))
- label1=Label(top,image=my_img1).place(relx=0.01,rely=0.01)
- button2=Button(top,text="Close window",bg='#ff4a65',command=top.destroy, relief=GROOVE).place(relx=0.9,rely=0.9)
- # Marks window as used
- hiddenwindow=1
- pickle.dump(hiddenwindow, open("window.dat", "wb"))
- Button(frame1, text="Developer Options", padx=57, bg="#86b3b3",fg="black", command = hidden).grid(row=2,column=0)
- top.mainloop(self.root)
- # Creates popup question window
- def popupQuestion():
- global responseYN
- responseYN = messagebox.askyesno("ITExtra Popup", "Would you like to read the user guide?")
- if responseYN == 1:
- popupInfo()
- def hide():
- if pickle.load(open("window.dat","rb")) == 1:
- hiddenwindow = 0
- pickle.dump(hiddenwindow, open("window.dat", "wb"))
- # Checks if battery state changed, in a function.
- def bState():
- t = IntVar()
- plugged = battery.power_plugged
- if plugged != t:
- changeRR(plugged)
- t = plugged
- self.root.after(300, bState) # reschedule event in 2 seconds
- # Sets the minimum refresh rate in ChangeRR function
- def rrChangeMin():
- global rrMin
- try:
- rrMin = int(e1.get())
- pickle.dump(rrMin, open("rrMin.dat", "wb"))
- except:
- popupError()
- def setMax():
- try:
- dllStuff(e2.get())
- rrMax = int(e2.get())
- except:
- try:
- dllStuff(pickle.load(open("rrMax.dat","rb")))
- except:
- popupError
- def setMin():
- try:
- dllStuff(e1.get())
- rrMin = int(e1.get())
- except:
- popupError()
- # Sets the maximum refresh rate in ChangeRR function
- def rrChangeMax():
- global rrMax
- try:
- rrMax = int(e2.get())
- pickle.dump(rrMax, open("rrMax.dat", "wb"))
- except:
- if str(e2.get())=="hidden":
- hidden()
- elif str(e2.get())=="hide":
- hide()
- else:
- popupError()
- def quit_window(icon, item):
- icon.stop()
- self.root.destroy()
- global didQuit
- didQuit=1
- self.root.protocol("WM_DELETE_WINDOW", self.callback)
- exitApp()
- # Define a function to show the window again
- def show_window(icon, item):
- icon.stop()
- self.root.after(0,self.root.deiconify())
- self.root.after(0, self.root.focus_force)
- def halfbright():
- sbc.set_brightness(80)
- def fullbright():
- sbc.set_brightness(100)
- # Hide the window and show on the system taskbar
- def hide_window():
- self.root.withdraw()
- image=Image.open("screen.ico")
- SEPARATOR = item('____', None)
- try:
- if pickle.load(open("window.dat","rb")) ==1:
- menu=( item('Set brightness to full', fullbright),
- item('Set brightness to 80', halfbright),
- item('Show developer menu',lambda: hidden(self.root)),
- item(SEPARATOR, None),
- item('Set to MAX RR', setMax),
- item('Set to min RR', setMin),
- item(SEPARATOR, None),
- item('Show App', show_window),
- item('Exit app', quit_window))
- else:
- menu=( item('Set brightness to full', fullbright),
- item('Set brightness to 80', halfbright),
- item(SEPARATOR, None),
- item('Set to MAX RR', setMax),
- item('Set to min RR', setMin),
- item(SEPARATOR, None),
- item('Show App', show_window),
- item('Exit app', quit_window))
- except:
- menu=( item('Set brightness to full', fullbright),
- item('Set brightness to 80', halfbright),
- item(SEPARATOR, None),
- item('Set to MAX RR', setMax),
- item('Set to min RR', setMin),
- item(SEPARATOR, None),
- item('Show App', show_window),
- item('Exit app', quit_window))
- icon=pystray.Icon("ITExtra", image, "RRChange", menu)
- icon.run()
- #_____________________________________________________________________________________________________________________________
- self.root = Tk()
- self.root.attributes('-alpha', 0.95)
- self.root.title("ITextra - Refresh Rate Change")
- # Set root geometry size
- w = 1100 # width for the Tk root
- h = 450 # height for the Tk root
- # get screen width and height
- ws = self.root.winfo_screenwidth() # width of the screen
- hs = self.root.winfo_screenheight() # height of the screen
- # calculate x and y coordinates for the Tk root window
- x = (ws/2) - (w/2)
- y = (hs/3) - (h/2)
- # set the dimensions of the screen
- # and where it is placed
- self.root.geometry('%dx%d+%d+%d' % (w, h, x, y))
- self.root.config(bg='#93C4C4')
- self.root.resizable(False, False)
- frame=LabelFrame(self.root)
- frame.config(bg='#93C4C4')
- self.root.iconbitmap('screen.ico')
- didQuit=0
- frame1=LabelFrame(self.root)
- frame1.config(bg='#93C4C4', bd=0)
- # Creates company logo in window
- my_img1 = ImageTk.PhotoImage(Image.open('ITEXTRA.png'))
- label1 = Label(image=my_img1)
- label1.config(bg='#93C4C4')
- label1.place(relx=0.01,rely=0)
- # Define and create textboxes.
- e1 = Entry(frame, width=50, borderwidth=5, bg='#C5FFFE', fg="black")
- e1.grid(row=0, column=0)
- e1.insert(0,pickle.load(open("rrMin.dat","rb")))
- e2 = Entry(frame, width=50, borderwidth=5, bg='#C5FFFE', fg="black")
- e2.grid(row=0, column=1)
- e2.insert(0,pickle.load(open("rrMax.dat","rb")))
- # Checkbox function
- def show():
- global startMinS
- try:
- startMinS = startMin.get()
- pickle.dump(startMinS, open("startMin.dat", "wb"))
- except:
- return
- # Create save file, or set startMin from savefile.
- try:
- startMin = IntVar()
- startMin.set(pickle.load(open("startMin.dat","rb")))
- except:
- startMin = 0
- pickle.dump(startMin, open("startMin.dat", "wb"))
- # Define and create checkbox
- check=Checkbutton(frame1, text="Do you want to start minimized?", bg='#93C4C4', variable = startMin,command=show)
- startMinS = startMin.get()
- if startMinS == 1:
- hide_window()
- # Checkbox function
- def show2():
- global Bness
- try:
- Bness = brightness.get()
- pickle.dump(Bness, open("brightness.dat", "wb"))
- except:
- return
- # Create save file, or set startMin from savefile.
- try:
- brightness = IntVar()
- brightness.set(pickle.load(open("brightness.dat","rb")))
- except:
- startMin = 0
- pickle.dump(startMin, open("brightness.dat", "wb"))
- # Define and create checkbox
- check2=Checkbutton(frame1, text="Change brightness upon charger (dis)connect", bg='#93C4C4', variable = brightness,command=show2)
- Bness = brightness.get()
- # Define and create buttons
- butMin = Button(frame, text="Set minimum", padx=40, pady=20, bg="#263D42",fg="white", command = rrChangeMin)
- butMax = Button(frame, text="Set maximum", padx=40, pady=20, bg="#263D42",fg="white", command = rrChangeMax)
- butReadme = Button(frame1, text="Show guide", padx=75, pady=20, bg="#86b3b3",fg="black", command = popupInfo)
- butMaxNow = Button(frame1, text="Set to maximum refresh-\nrate immediately.", padx=40, pady=20, bg="#86b3b3",fg="black", command = setMax)
- CloseWin=Button(self.root,text="Close window", bg='#ff4a65',command=hide_window, relief=GROOVE).place(relx=0.9,rely=0.9)
- def callback(url):
- webbrowser.open_new(url)
- link1 = Label(self.root, text="Website - ITextra", fg="blue", bg='#93C4C4', cursor="hand2")
- link1.place(relx=0.48,rely=0.9)
- link1.bind("<Button-1>", lambda e: callback("https://itextra.nl"))
- emptyLabel = Label(self.root,text=" ", bg='#93C4C4').grid(row=2,column=4)
- try:
- hiddenwindow = pickle.load(open("window.dat","rb"))
- if hiddenwindow == 1:
- butHidden = Button(frame1, text="Developer Options", padx=57, bg="#86b3b3",fg="black", command = hidden).grid(row=2,column=0)
- except:
- hiddenwindow = 0
- frame.place(relx=0.25,rely=0.6)
- frame1.place(relx=0,rely=0.5)
- butMin.grid(row=1, column=0)
- butMax.grid(row=1, column=1)
- butReadme.grid(row=0, column=0)
- butMaxNow.grid(row=1, column=0)
- check.grid(row=3, column=0,sticky='w')
- check2.grid(row=4, column=0,sticky='w')
- self.root.protocol('WM_DELETE_WINDOW', hide_window)
- # self.root.after(300, bState)
- self.root.mainloop()
- #_____________________________________________________________________________________________________________________________
- # As name implies, calls a dll and changes the refresh rate to the 'rr' variable
- def dllStuff (rr):
- devmode = pywintypes.DEVMODEType()
- devmode.DisplayFrequency = int(rr)
- devmode.Fields = win32con.DM_DISPLAYFREQUENCY
- win32api.ChangeDisplaySettings(devmode, 0)
- #_____________________________________________________________________________________________________________________________
- # Checks if battery charging or not, changes refresh rate accordingly
- def changeRR (bool):
- try:
- if plugged == True:
- dllStuff(pickle.load(open("rrMax.dat","rb")))
- if pickle.load(open("brightness.dat","rb"))==1:
- sbc.set_brightness(100)
- elif plugged == False:
- dllStuff(pickle.load(open("rrMin.dat","rb")))
- if pickle.load(open("brightness.dat","rb"))==1:
- sbc.set_brightness(80)
- except:
- if plugged == True:
- dllStuff(144)
- elif plugged == False:
- dllStuff(60)
- #_____________________________________________________________________________________________________________________________
- didQuit=0
- app = App()
- t = bool
- while didQuit!=1:
- time.sleep(1)
- battery = psutil.sensors_battery()
- plugged = battery.power_plugged
- if plugged != t:
- changeRR(battery.power_plugged)
- t = plugged
- def exitApp():
- global didQuit
- didQuit=1
- exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement