Helpex - Trao đổi & giúp đỡ Đăng nhập
60

Làm cách nào để biết cửa sổ Tkinter cần mở ở đâu, dựa trên kích thước màn hình? Tôi muốn nó mở ở giữa.

60 hữu ích 1 bình luận 96k xem chia sẻ
91

Câu trả lời này dựa trên câu trả lời của Rachel . Mã của cô ấy ban đầu không hoạt động, nhưng với một số chỉnh sửa, tôi có thể sửa lỗi.

import tkinter as tk


root = tk.Tk() # create a Tk root window

w = 800 # width for the Tk root
h = 650 # height for the Tk root

# get screen width and height
ws = root.winfo_screenwidth() # width of the screen
hs = root.winfo_screenheight() # height of the screen

# calculate x and y coordinates for the Tk root window
x = (ws/2) - (w/2)
y = (hs/2) - (h/2)

# set the dimensions of the screen 
# and where it is placed
root.geometry('%dx%d+%d+%d' % (w, h, x, y))

root.mainloop() # starts the mainloop
91 hữu ích 0 bình luận chia sẻ
40

Thử cái này

import tkinter as tk


def center_window(width=300, height=200):
    # get screen width and height
    screen_width = root.winfo_screenwidth()
    screen_height = root.winfo_screenheight()

    # calculate position x and y coordinates
    x = (screen_width/2) - (width/2)
    y = (screen_height/2) - (height/2)
    root.geometry('%dx%d+%d+%d' % (width, height, x, y))


root = tk.Tk()
center_window(500, 400)
root.mainloop()

Nguồn

40 hữu ích 5 bình luận chia sẻ
24
root.geometry('250x150+0+0')

Hai tham số đầu tiên là chiều rộng và chiều cao của cửa sổ. Hai tham số cuối cùng là tọa độ màn hình x và y. Bạn có thể chỉ định tọa độ x và y bắt buộc

24 hữu ích 1 bình luận chia sẻ
3

Nếu bạn muốn cửa sổ được căn giữa.

Sau đó, loại chức năng này có thể giúp bạn -:

def center_window(size, window) :
    window_width = size[0] #Fetches the width you gave as arg. Alternatively window.winfo_width can be used if width is not to be fixed by you.
    window_height = size[1] #Fetches the height you gave as arg. Alternatively window.winfo_height can be used if height is not to be fixed by you.
    window_x = int((window.winfo_screenwidth() / 2) - (window_width / 2)) #Calculates the x for the window to be in the centre
    window_y = int((window.winfo_screenheight() / 2) - (window_height / 2)) #Calculates the y for the window to be in the centre

    window_geometry = str(window_width) + 'x' + str(window_height) + '+' + str(window_x) + '+' + str(window_y) #Creates a geometric string argument
    window.geometry(window_geometry) #Sets the geometry accordingly.
    return

Ở đây, hàm window.winfo_screenwidth được sử dụng để lấy độ rộng của màn hình thiết bị. Và hàm window.winfo_screenheight được sử dụng để lấy chiều cao của màn hình thiết bị.

Chúng có thể được gọi trên cửa sổ Tk của python.

Ở đây bạn có thể gọi hàm này và chuyển một bộ giá trị với kích thước (chiều rộng, chiều cao) của màn hình.

Bạn có thể tùy chỉnh phép tính nhiều như bạn muốn và nó sẽ thay đổi theo.

3 hữu ích 1 bình luận chia sẻ
1

root.geometry('520x400+350+200')

Giải thích: ('rộng x cao + tọa độ X + tọa độ Y')

1 hữu ích 1 bình luận chia sẻ
loading
Không tìm thấy câu trả lời bạn tìm kiếm? Duyệt qua các câu hỏi được gắn thẻ python python-2.7 tkinter , hoặc hỏi câu hỏi của bạn.

Có thể bạn quan tâm

loading