create a tkinter windows 300 width and 200 height with white background with text in inside and ok exit button at the bottom of the window in python

Here is an example code that creates a Tkinter window with the given specifications:

main.py
import tkinter as tk

# Create the root window
root = tk.Tk()

# Set the window size
root.geometry("300x200")

# Set the window background color
root.config(bg="white")

# Create a label widget with some text
lbl_text = tk.Label(root, text="Hello World!")
lbl_text.pack(pady=20)

# Create an "OK" button to exit the window
btn_ok = tk.Button(root, text="OK", command=root.destroy)
btn_ok.pack(side=tk.BOTTOM, pady=10)

# Start the Tkinter main loop
root.mainloop()
465 chars
22 lines

We first import the tkinter module and create the root window using the Tk() constructor. We then set the window size and background color using the geometry() and config() methods respectively.

Next, we create a label widget with some text using the Label() constructor and pack it in the root window using the pack() method.

Finally, we create an "OK" button using the Button() constructor, set its text and command to close the window, and pack it at the bottom of the window.

We start the Tkinter main loop using the mainloop() method on the root window.

related categories

gistlibby LogSnag