[Python] 14. TKINTER-layout(absolute)

Min's Study Noteยท2023๋…„ 11์›” 9์ผ

Python

๋ชฉ๋ก ๋ณด๊ธฐ
15/21

๐Ÿ”ณ place() : ์ ˆ๋Œ€ ์œ„์น˜๋กœ ๋ฐฐ์น˜๋ฅผ ํ•  ์ˆ˜ ์žˆ๋Š” ๋ ˆ์ด์•„์›ƒ

import tkinter
import tkinter.ttk as ttk
import tkinter.font
from tkinter import *

root = Tk()
root.title('MyWindow layout grid')
root.geometry('400x400')
root.config(bg="white")
## resizable์€ 0,1 ๋˜๋Š” True, False ์‚ฌ์šฉ
root.resizable(1, 1)
font = tkinter.font.Font(family="๋ง‘์€ ๊ณ ๋”•", size=13)

## place() ์ ˆ๋Œ€ ์œ„์น˜๋กœ ๋ฐฐ์น˜๋ฅผ ํ•  ์ˆ˜ ์žˆ๋Š” ๋ ˆ์ด์•„์›ƒ
labelId = tkinter.Label(root, text="ID", font=font, background="white")
entryId = tkinter.Entry(root, relief="solid", borderwidth="1", font=font)
labelPass = tkinter.Label(root, text="Pass", font=font, background="white")
entryPass = tkinter.Entry(root, relief="solid", borderwidth="1", font=font, show="*")

labelId.place(x=10, y=8)
entryId.place(x=50, y=10)
labelPass.place(x=10, y=38)
entryPass.place(x=50, y=40)

date = [str(i)+"์ผ" for i in range(1,32)]
combo = ttk.Combobox(root, height=5, values=date, state="readonly")
combo.place(x=10, y=80)

root.mainloop()

๐Ÿ‘‰ ์œ„์น˜ ์„ค์ •ํ•  ๋•Œ ์œ ์šฉํ•œ ํŒŒ์ด์ฌ ์ฝ”๋“œ

import tkinter as tk

class CoordinateTool:
    def __init__(self, root):
        self.root = root

        # ๋ผ๋ฒจ ์ดˆ๊ธฐํ™”
        self.label = tk.Label(root, text="๋งˆ์šฐ์Šค๋กœ ํด๋ฆญํ•˜์„ธ์š”!")
        self.label.pack(pady=10)

        # ๋งˆ์šฐ์Šค ํด๋ฆญ ์ด๋ฒคํŠธ ๋ฐ”์ธ๋”ฉ
        self.root.bind("<Button-1>", self.on_click)

        # ์ฐฝ ํฌ๊ธฐ ์กฐ์ ˆ ์ด๋ฒคํŠธ ๋ฐ”์ธ๋”ฉ
        self.root.bind("<Configure>", self.on_resize)

    def on_click(self, event):
        # ๋งˆ์šฐ์Šค ํด๋ฆญ ์‹œ ํ˜ธ์ถœ๋˜๋Š” ํ•จ์ˆ˜
        click_position_str = f"ํด๋ฆญ ์œ„์น˜: {event.x}, {event.y}"
        self.label.config(text=click_position_str)

    def on_resize(self, event):
        # ์ฐฝ ํฌ๊ธฐ ์กฐ์ ˆ ์‹œ ํ˜ธ์ถœ๋˜๋Š” ํ•จ์ˆ˜
        window_size_str = f"์ฐฝ ํฌ๊ธฐ: {root.winfo_width()} x {root.winfo_height()}"
        self.root.title(window_size_str)

if __name__ == "__main__":
    root = tk.Tk()
    app = CoordinateTool(root)
    root.geometry("300x200")  # ์ดˆ๊ธฐ ์ฐฝ ํฌ๊ธฐ
    root.mainloop()

0๊ฐœ์˜ ๋Œ“๊ธ€