Advertisement
Guest User

Untitled

a guest
Jan 26th, 2022
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.88 KB | None | 0 0
  1. # Run tkinter code in another thread
  2.  
  3. from tkinter import *
  4. from tkinter import messagebox, messagebox
  5. from tkinter.ttk import Separator
  6. from tracemalloc import start
  7. from pystray import MenuItem as item
  8. import pystray
  9. from PIL import ImageTk,Image
  10. import screen_brightness_control as sbc
  11. import win32api
  12. import win32con
  13. import pywintypes
  14. import psutil
  15. import time
  16. import threading
  17. import pickle
  18. import webbrowser
  19. import sys
  20. import os
  21.  
  22. os.chdir(sys.path[0])
  23. class App(threading.Thread):
  24. def __init__(self):
  25. threading.Thread.__init__(self)
  26. self.start()
  27.  
  28. def callback(self):
  29. self.root.quit()
  30.  
  31.  
  32. def run(self):
  33.  
  34. # Define a function for quit the window
  35. def popupInfo():
  36. 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")
  37.  
  38. # Creates popup error window
  39. def popupError():
  40. messagebox.showerror("ITExtra RR Change", "Error.")
  41.  
  42. def hidden(root):
  43. global my_img1
  44. top=Toplevel(root)
  45. top.title("Secret menu, shhh :^)")
  46. top.overrideredirect(True)
  47. top.attributes('-alpha', 0.9)
  48. # Set root geometry size
  49. w = 1100 # width for the Tk root
  50. h = 450 # height for the Tk root
  51. # get screen width and height
  52. ws = top.winfo_screenwidth() # width of the screen
  53. hs = top.winfo_screenheight() # height of the screen
  54. # calculate x and y coordinates for the Tk root window
  55. x = (ws/2) - (w/2)
  56. y = (hs/3) - (h/2)
  57. # set the dimensions of the screen
  58. # and where it is placed
  59. top.geometry('%dx%d+%d+%d' % (w, h, x, y))
  60.  
  61. top.iconbitmap('screen.ico')
  62. my_img1 = ImageTk.PhotoImage(Image.open("ITEXTRA.png"))
  63. label1=Label(top,image=my_img1).place(relx=0.01,rely=0.01)
  64. button2=Button(top,text="Close window",bg='#ff4a65',command=top.destroy, relief=GROOVE).place(relx=0.9,rely=0.9)
  65.  
  66. # Marks window as used
  67. hiddenwindow=1
  68. pickle.dump(hiddenwindow, open("window.dat", "wb"))
  69. Button(frame1, text="Developer Options", padx=57, bg="#86b3b3",fg="black", command = hidden).grid(row=2,column=0)
  70. top.mainloop(self.root)
  71.  
  72. # Creates popup question window
  73. def popupQuestion():
  74. global responseYN
  75. responseYN = messagebox.askyesno("ITExtra Popup", "Would you like to read the user guide?")
  76. if responseYN == 1:
  77. popupInfo()
  78.  
  79. def hide():
  80. if pickle.load(open("window.dat","rb")) == 1:
  81. hiddenwindow = 0
  82. pickle.dump(hiddenwindow, open("window.dat", "wb"))
  83.  
  84. # Checks if battery state changed, in a function.
  85. def bState():
  86. t = IntVar()
  87. plugged = battery.power_plugged
  88. if plugged != t:
  89. changeRR(plugged)
  90. t = plugged
  91. self.root.after(300, bState) # reschedule event in 2 seconds
  92.  
  93. # Sets the minimum refresh rate in ChangeRR function
  94. def rrChangeMin():
  95. global rrMin
  96. try:
  97. rrMin = int(e1.get())
  98. pickle.dump(rrMin, open("rrMin.dat", "wb"))
  99. except:
  100. popupError()
  101.  
  102. def setMax():
  103. try:
  104. dllStuff(e2.get())
  105. rrMax = int(e2.get())
  106. except:
  107. try:
  108. dllStuff(pickle.load(open("rrMax.dat","rb")))
  109. except:
  110. popupError
  111. def setMin():
  112. try:
  113. dllStuff(e1.get())
  114. rrMin = int(e1.get())
  115. except:
  116. popupError()
  117.  
  118. # Sets the maximum refresh rate in ChangeRR function
  119. def rrChangeMax():
  120. global rrMax
  121. try:
  122. rrMax = int(e2.get())
  123. pickle.dump(rrMax, open("rrMax.dat", "wb"))
  124. except:
  125. if str(e2.get())=="hidden":
  126. hidden()
  127. elif str(e2.get())=="hide":
  128. hide()
  129. else:
  130. popupError()
  131.  
  132. def quit_window(icon, item):
  133. icon.stop()
  134. self.root.destroy()
  135. global didQuit
  136. didQuit=1
  137. self.root.protocol("WM_DELETE_WINDOW", self.callback)
  138. exitApp()
  139.  
  140. # Define a function to show the window again
  141.  
  142. def show_window(icon, item):
  143. icon.stop()
  144. self.root.after(0,self.root.deiconify())
  145. self.root.after(0, self.root.focus_force)
  146.  
  147. def halfbright():
  148. sbc.set_brightness(80)
  149.  
  150. def fullbright():
  151. sbc.set_brightness(100)
  152.  
  153. # Hide the window and show on the system taskbar
  154. def hide_window():
  155. self.root.withdraw()
  156. image=Image.open("screen.ico")
  157. SEPARATOR = item('____', None)
  158. try:
  159. if pickle.load(open("window.dat","rb")) ==1:
  160. menu=( item('Set brightness to full', fullbright),
  161. item('Set brightness to 80', halfbright),
  162. item('Show developer menu',lambda: hidden(self.root)),
  163. item(SEPARATOR, None),
  164. item('Set to MAX RR', setMax),
  165. item('Set to min RR', setMin),
  166. item(SEPARATOR, None),
  167. item('Show App', show_window),
  168. item('Exit app', quit_window))
  169. else:
  170. menu=( item('Set brightness to full', fullbright),
  171. item('Set brightness to 80', halfbright),
  172. item(SEPARATOR, None),
  173. item('Set to MAX RR', setMax),
  174. item('Set to min RR', setMin),
  175. item(SEPARATOR, None),
  176. item('Show App', show_window),
  177. item('Exit app', quit_window))
  178. except:
  179. menu=( item('Set brightness to full', fullbright),
  180. item('Set brightness to 80', halfbright),
  181. item(SEPARATOR, None),
  182. item('Set to MAX RR', setMax),
  183. item('Set to min RR', setMin),
  184. item(SEPARATOR, None),
  185. item('Show App', show_window),
  186. item('Exit app', quit_window))
  187.  
  188. icon=pystray.Icon("ITExtra", image, "RRChange", menu)
  189. icon.run()
  190. #_____________________________________________________________________________________________________________________________
  191.  
  192. self.root = Tk()
  193. self.root.attributes('-alpha', 0.95)
  194. self.root.title("ITextra - Refresh Rate Change")
  195.  
  196. # Set root geometry size
  197. w = 1100 # width for the Tk root
  198. h = 450 # height for the Tk root
  199.  
  200. # get screen width and height
  201. ws = self.root.winfo_screenwidth() # width of the screen
  202. hs = self.root.winfo_screenheight() # height of the screen
  203.  
  204. # calculate x and y coordinates for the Tk root window
  205. x = (ws/2) - (w/2)
  206. y = (hs/3) - (h/2)
  207.  
  208. # set the dimensions of the screen
  209. # and where it is placed
  210. self.root.geometry('%dx%d+%d+%d' % (w, h, x, y))
  211.  
  212.  
  213. self.root.config(bg='#93C4C4')
  214. self.root.resizable(False, False)
  215. frame=LabelFrame(self.root)
  216. frame.config(bg='#93C4C4')
  217. self.root.iconbitmap('screen.ico')
  218. didQuit=0
  219. frame1=LabelFrame(self.root)
  220. frame1.config(bg='#93C4C4', bd=0)
  221.  
  222.  
  223. # Creates company logo in window
  224.  
  225. my_img1 = ImageTk.PhotoImage(Image.open('ITEXTRA.png'))
  226. label1 = Label(image=my_img1)
  227. label1.config(bg='#93C4C4')
  228. label1.place(relx=0.01,rely=0)
  229.  
  230.  
  231. # Define and create textboxes.
  232.  
  233. e1 = Entry(frame, width=50, borderwidth=5, bg='#C5FFFE', fg="black")
  234. e1.grid(row=0, column=0)
  235. e1.insert(0,pickle.load(open("rrMin.dat","rb")))
  236.  
  237.  
  238. e2 = Entry(frame, width=50, borderwidth=5, bg='#C5FFFE', fg="black")
  239. e2.grid(row=0, column=1)
  240. e2.insert(0,pickle.load(open("rrMax.dat","rb")))
  241.  
  242. # Checkbox function
  243. def show():
  244. global startMinS
  245. try:
  246. startMinS = startMin.get()
  247. pickle.dump(startMinS, open("startMin.dat", "wb"))
  248. except:
  249. return
  250.  
  251. # Create save file, or set startMin from savefile.
  252. try:
  253. startMin = IntVar()
  254. startMin.set(pickle.load(open("startMin.dat","rb")))
  255. except:
  256. startMin = 0
  257. pickle.dump(startMin, open("startMin.dat", "wb"))
  258.  
  259. # Define and create checkbox
  260. check=Checkbutton(frame1, text="Do you want to start minimized?", bg='#93C4C4', variable = startMin,command=show)
  261. startMinS = startMin.get()
  262. if startMinS == 1:
  263. hide_window()
  264.  
  265. # Checkbox function
  266. def show2():
  267. global Bness
  268. try:
  269. Bness = brightness.get()
  270. pickle.dump(Bness, open("brightness.dat", "wb"))
  271. except:
  272. return
  273. # Create save file, or set startMin from savefile.
  274. try:
  275. brightness = IntVar()
  276. brightness.set(pickle.load(open("brightness.dat","rb")))
  277. except:
  278. startMin = 0
  279. pickle.dump(startMin, open("brightness.dat", "wb"))
  280. # Define and create checkbox
  281. check2=Checkbutton(frame1, text="Change brightness upon charger (dis)connect", bg='#93C4C4', variable = brightness,command=show2)
  282. Bness = brightness.get()
  283.  
  284. # Define and create buttons
  285. butMin = Button(frame, text="Set minimum", padx=40, pady=20, bg="#263D42",fg="white", command = rrChangeMin)
  286. butMax = Button(frame, text="Set maximum", padx=40, pady=20, bg="#263D42",fg="white", command = rrChangeMax)
  287. butReadme = Button(frame1, text="Show guide", padx=75, pady=20, bg="#86b3b3",fg="black", command = popupInfo)
  288. butMaxNow = Button(frame1, text="Set to maximum refresh-\nrate immediately.", padx=40, pady=20, bg="#86b3b3",fg="black", command = setMax)
  289. CloseWin=Button(self.root,text="Close window", bg='#ff4a65',command=hide_window, relief=GROOVE).place(relx=0.9,rely=0.9)
  290.  
  291. def callback(url):
  292. webbrowser.open_new(url)
  293. link1 = Label(self.root, text="Website - ITextra", fg="blue", bg='#93C4C4', cursor="hand2")
  294. link1.place(relx=0.48,rely=0.9)
  295. link1.bind("<Button-1>", lambda e: callback("https://itextra.nl"))
  296.  
  297. emptyLabel = Label(self.root,text=" ", bg='#93C4C4').grid(row=2,column=4)
  298.  
  299. try:
  300. hiddenwindow = pickle.load(open("window.dat","rb"))
  301. if hiddenwindow == 1:
  302. butHidden = Button(frame1, text="Developer Options", padx=57, bg="#86b3b3",fg="black", command = hidden).grid(row=2,column=0)
  303. except:
  304. hiddenwindow = 0
  305.  
  306. frame.place(relx=0.25,rely=0.6)
  307. frame1.place(relx=0,rely=0.5)
  308.  
  309.  
  310. butMin.grid(row=1, column=0)
  311. butMax.grid(row=1, column=1)
  312.  
  313. butReadme.grid(row=0, column=0)
  314. butMaxNow.grid(row=1, column=0)
  315. check.grid(row=3, column=0,sticky='w')
  316. check2.grid(row=4, column=0,sticky='w')
  317. self.root.protocol('WM_DELETE_WINDOW', hide_window)
  318.  
  319. # self.root.after(300, bState)
  320. self.root.mainloop()
  321. #_____________________________________________________________________________________________________________________________
  322.  
  323. # As name implies, calls a dll and changes the refresh rate to the 'rr' variable
  324. def dllStuff (rr):
  325. devmode = pywintypes.DEVMODEType()
  326. devmode.DisplayFrequency = int(rr)
  327. devmode.Fields = win32con.DM_DISPLAYFREQUENCY
  328. win32api.ChangeDisplaySettings(devmode, 0)
  329. #_____________________________________________________________________________________________________________________________
  330.  
  331. # Checks if battery charging or not, changes refresh rate accordingly
  332. def changeRR (bool):
  333. try:
  334. if plugged == True:
  335. dllStuff(pickle.load(open("rrMax.dat","rb")))
  336. if pickle.load(open("brightness.dat","rb"))==1:
  337. sbc.set_brightness(100)
  338. elif plugged == False:
  339. dllStuff(pickle.load(open("rrMin.dat","rb")))
  340. if pickle.load(open("brightness.dat","rb"))==1:
  341. sbc.set_brightness(80)
  342. except:
  343. if plugged == True:
  344. dllStuff(144)
  345. elif plugged == False:
  346. dllStuff(60)
  347.  
  348. #_____________________________________________________________________________________________________________________________
  349. didQuit=0
  350. app = App()
  351. t = bool
  352.  
  353. while didQuit!=1:
  354. time.sleep(1)
  355. battery = psutil.sensors_battery()
  356. plugged = battery.power_plugged
  357. if plugged != t:
  358. changeRR(battery.power_plugged)
  359. t = plugged
  360.  
  361. def exitApp():
  362. global didQuit
  363. didQuit=1
  364. exit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement