Răsfoiți Sursa

美化了初始屏幕

Huan 5 ani în urmă
părinte
comite
3ed35b92c6
2 a modificat fișierele cu 16 adăugiri și 5 ștergeri
  1. 3 2
      Hello.py
  2. 13 3
      newtkinter/__init__.py

+ 3 - 2
Hello.py

@@ -10,7 +10,7 @@ import os
 import tkinter.messagebox
 import webbrowser
 
-from newtkinter import DragWindow
+from newtkinter import DragWindow, center_windows
 
 img = None
 SCREEN = None
@@ -384,7 +384,7 @@ def close():
 
 def cotan_main():
     global SCREEN, title_color, button_color, button_cursor, img
-    SCREEN = DragWindow(alpha=0.97, width=1200, height=800)
+    SCREEN = DragWindow(width=1200, height=800)
     font1 = tkfont.Font(family='Comic Sans MS', size=20, weight=tkfont.BOLD)
     font2 = tkfont.Font(family='Comic Sans MS', size=16, weight=tkfont.BOLD)
     font3 = tkfont.Font(family='Comic Sans MS', size=10)
@@ -392,6 +392,7 @@ def cotan_main():
     SCREEN.title('')
     SCREEN.resizable(width=False, height=False)
     SCREEN.geometry(f'1200x800+30+30')
+    center_windows(SCREEN, 1200, 800)
     # 渲染白色
     frame = tkinter.Frame(SCREEN, width=1200, height=800, bg='#FFFFFF')
     frame.pack()

+ 13 - 3
newtkinter/__init__.py

@@ -2,16 +2,26 @@ import tkinter as tk
 from tkinter import filedialog
 
 
+def center_windows(screen, w, h):
+    ws = screen.winfo_screenwidth()
+    hs = screen.winfo_screenheight()
+    # 计算 x, y 位置
+    x = (ws/2) - (w/2) + 1
+    y = (hs/2) - (h/2) + 1
+    screen.geometry('%dx%d+%d+%d' % (w, h, x, y))
+
+
 class DragWindow(tk.Tk):
     root_x, root_y, abs_x, abs_y = 0, 0, 0, 0
     width, height = None, None
 
-    def __init__(self, alpha=0.97, width=None, height=None):
+    def __init__(self, alpha=1, width=None, height=None, drag=False):
         super().__init__()
         self.width, self.height = width, height
         self.wm_attributes("-alpha", alpha)  # 透明度
-        self.bind("<B1-Motion>", self._on_move)
-        self.bind("<ButtonPress-1>", self._on_tap)
+        if drag:
+            self.bind("<B1-Motion>", self._on_move)
+            self.bind("<ButtonPress-1>", self._on_tap)
 
     def set_display_postion(self, offset_x, offset_y):
         self.geometry("+%s+%s" % (offset_x, offset_y))