设置窗口无边框的方法是用overrideredirect(True),代码如下:
import tkinter as tk
root = tk.Tk()
root.overrideredirect(True)
root.mainloop()
别急划走~~,如果这样设置,状态栏的小图标没有了~。如果你时window系统,我们换个方法,用window的原生接口来解决无边框问题。
方法一:
from tkinter import Tk
from ctypes import Structure, c_long, windll, byref
class RECT(Structure):
_fields_ = [("left", c_long), ("top", c_long), ("right", c_long), ("bottom", c_long)]
SPI_GETWORKAREA = 48
GWL_STYLE = -16 # 用于获取窗口样式
WS_CAPTION = 12582912 # 窗口标题栏样式
WS_THICKFRAME = 262144 # 窗口可调整边框样式
class WindowTk(Tk):
def __init__(self, width=1000, height=600):
super().__init__()
'''居中显示'''
work_rect = RECT()
windll.user32.SystemParametersInfoW(SPI_GETWORKAREA, 0, byref(work_rect), 0)
self.ww = work_rect.right - work_rect.left
self.wh = work_rect.bottom - work_rect.top
size = '%dx%d+%d+%d' % (width, height, (self.ww - width) / 2, (self.wh - height) / 2)
self.geometry(size)
'''无边框'''
self.update_idletasks() # 刷新窗口状态
self.hwnd = windll.user32.GetParent(self.winfo_id()) # 获取窗口句柄
style = windll.user32.GetWindowLongPtrW(self.hwnd, GWL_STYLE)
style &= ~(WS_CAPTION | WS_THICKFRAME)
windll.user32.SetWindowLongPtrW(self.hwnd, GWL_STYLE, style)
if __name__ == '__main__':
root = WindowTk()
root.mainloop()
方法二:
import tkinter as tk
from ctypes import windll
class BorderlessWindow(tk.Tk):
def __init__(self):
super().__init__()
self.overrideredirect(True)
self.iconbitmap("logo.ico") # 图标
hwnd = windll.user32.GetParent(self.winfo_id())
windll.user32.ShowWindow(hwnd, 0) # SW_HIDE
# 修改窗口样式
ex_style = windll.user32.GetWindowLongPtrW(hwnd, -20) # GWL_EXSTYLE
windll.user32.SetWindowLongPtrW(hwnd, -20, ex_style | 262144) # WS_EX_APPWINDOW
# 设置单击任务栏图标可以最小化
style = windll.user32.GetWindowLongPtrW(hwnd, -16) # GWL_STYLE
windll.user32.SetWindowLongPtrW(hwnd, -16, style | 131072) # WS_MINIMIZEBOX
# 刷新任务栏
windll.user32.ShowWindow(hwnd, 5) # SW_SHOW
if __name__ == "__main__":
app = BorderlessWindow()
app.geometry("400x300+500+200")
app.mainloop()
OK~~
1063

被折叠的 条评论
为什么被折叠?



