如何使用def centerscreen将所有屏幕居中

时间:2019-04-23 07:12:15

标签: python tkinter

我有一个代码可以调用其他窗口。我创建一个def center_screen。但是当我调用该函数时,我需要将center_screen()应用于所有窗口,而不仅是一个窗口。

我有主人:

def center_screen():
    window_height = 150
    window_width = 200
    master.resizable(0, 0)
    screen_width = master.winfo_screenwidth()
    screen_height = master.winfo_screenheight()
    x_cordinate = int((screen_width/2) - (window_width/2))
    y_cordinate = int((screen_height/2) - (window_height/2))
    master.geometry("{}x{}+{}+{}".format(window_width, window_height, x_cordinate, y_cordinate))

和一个用于关闭母版并打开问候窗口的按钮

def helloCallBack():
    master.destroy()
    win_mysql = Tk()
    win_mysql.title ('MyApp')

    center_screen()

    # window_height = 150
    # window_width = 200
    # hello.resizable(0, 0)
    # screen_width = hello.winfo_screenwidth()
    # screen_height = hello.winfo_screenheight()
    # x_cordinate = int((screen_width/2) - (window_width/2))
    # y_cordinate = int((screen_height/2) - (window_height/2))
    # hello.geometry("{}x{}+{}+{}".format(window_width, window_height, x_cordinate, y_cordinate))

    w = Label(win_mysql, text="Hello World")
    w.pack()

我希望我在调用center_screen()时也可以打招呼。

我所有的代码:

import sys
import os
import tkinter as tk
from tkinter import *

#root = tk.Tk()

master=Tk()
master.title ('MyApp')
v = IntVar()

#select_db = v

#def current_screen():


def center_screen():
    window_height = 150
    window_width = 200
    master.resizable(0, 0)
    screen_width = master.winfo_screenwidth()
    screen_height = master.winfo_screenheight()
    x_cordinate = int((screen_width/2) - (window_width/2))
    y_cordinate = int((screen_height/2) - (window_height/2))
    master.geometry("{}x{}+{}+{}".format(window_width, window_height, x_cordinate, y_cordinate))

def select_db():
    v.get()

def helloCallBack():
    master.destroy()
    win_mysql = Tk()
    win_mysql.title ('MyApp')

    center_screen()

    # window_height = 150
    # window_width = 200
    # hello.resizable(0, 0)
    # screen_width = hello.winfo_screenwidth()
    # screen_height = hello.winfo_screenheight()
    # x_cordinate = int((screen_width/2) - (window_width/2))
    # y_cordinate = int((screen_height/2) - (window_height/2))
    # hello.geometry("{}x{}+{}+{}".format(window_width, window_height, x_cordinate, y_cordinate))

    w = Label(win_mysql, text="Hello World")
    w.pack()
    #hello.mainloop()

center_screen()

radio_1=Radiobutton(master, text="Create LOCAL DataBase", variable=v, value=1, command=select_db).pack(anchor=W)
radio_2=Radiobutton(master, text="Create MYSQL Database", variable=v, value=2, command=select_db).pack(anchor=W)
b1=tk.Button(master, text="OUVRIR",bg="white",command=helloCallBack)
b1.pack()
master.mainloop()

0 个答案:

没有答案