create a tkinter windows 300 width and 200 height with white background with text in inside and ok exit button in python

Here's the code to create a tkinter window of 300 width and 200 height, with a white background, text in the center, and an "OK" button to close the window:

main.py
import tkinter as tk

# Create the window
window = tk.Tk()
window.title("My Window")
window.geometry("300x200")
window.configure(bg="white")

# Add the text
label = tk.Label(window, text="Hello, World!", padx=20, pady=20)
label.pack()

# Add the "OK" button
button = tk.Button(window, text="OK", command=window.destroy)
button.pack()

# Start the event loop
window.mainloop()
376 chars
19 lines

In this code, we import the tkinter package and create a new window using Tk(). We set the title, size, and background color of the window using the title(), geometry(), and configure() functions.

Next, we add a Label widget to display the text "Hello, World!" in the center of the window, with some padding to make it look nicer. We pack the label into the window using the pack() function.

Finally, we add an OK button using a Button widget. The command parameter specifies that when the button is clicked, the destroy() method of the window should be called to close the window. We pack the button into the window as well.

Finally, we start the event loop using the mainloop() function to keep the window open and responsive to user events.

gistlibby LogSnag