核心代码
def windowCenter(self,width,height):
screenwidth = self.window.winfo_screenwidth()
screenheight = self.window.winfo_screenheight()
size = "%dx%d+%d+%d"%(width,height,screenwidth/2-width/2,screenheight/2-height/2)
self.window.geometry(size)
完整代码
import tkinter as tk
class MainWindow():
def __init__(self):
self.window = tk.Tk()
self.window.minsize(800, 750)
self.windowCenter(800,750)
self.window.title("连连看小游戏")
self.addComponents()
self.window.mainloop()
def windowCenter(self,width,height):
screenwidth = self.window.winfo_screenwidth()
screenheight = self.window.winfo_screenheight()
size = "%dx%d+%d+%d"%(width,height,screenwidth/2-width/2,screenheight/2-height/2)
self.window.geometry(size)
def addComponents(self):
my_frame = tk.Frame(self.window)
my_frame.pack(side=tk.TOP)
my_button = tk.Button(my_frame, text="连连看小游戏")
my_button.pack(side=tk.LEFT)
my_canvas = tk.Canvas(my_frame, bg='white')
my_canvas.create_rectangle(50, 50, 150, 150, outline='red', fill='blue', width=5)
if __name__ == "__main__":
MainWindow()