Skip to content
Snippets Groups Projects
Commit 4e6ed093 authored by Bui2.Huan@live.uwe.ac.uk's avatar Bui2.Huan@live.uwe.ac.uk
Browse files

Upload New File

parent 76d78d44
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id: tags:
4a
%% Cell type:code id: tags:
``` python
from tkinter import Tk, ttk
# Create main window
wndw = Tk()
wndw.title("Search Example")
wndw.geometry("300x150") # Set window size
# Create a frame
frm = ttk.Frame(wndw, padding=10)
frm.grid()
# Add a text field (Entry widget)
entry = ttk.Entry(frm, width=20)
entry.grid(column=0, row=0, padx=5, pady=5)
# Add a button (doesn't do anything yet)
ttk.Button(frm, text="SEARCH").grid(column=1, row=0, padx=5, pady=5)
# Start the event loop
wndw.mainloop()
```
%% Cell type:markdown id: tags:
4b
%% Cell type:code id: tags:
``` python
from tkinter import Tk, messagebox, ttk
# Function to capture and display text
def show_input():
user_input = entry.get() # Get text from entry field
messagebox.showinfo("Your Input", f"You entered: {user_input}")
# Create main window
wndw = Tk()
wndw.title("Search Example")
wndw.geometry("300x150")
# Create a frame
frm = ttk.Frame(wndw, padding=10)
frm.grid()
# Add a text field
entry = ttk.Entry(frm, width=20)
entry.grid(column=0, row=0, padx=5, pady=5)
# Add a button that captures input
ttk.Button(frm, text="SEARCH", command=show_input).grid(column=1, row=0, padx=5, pady=5)
# Start the event loop
wndw.mainloop()
```
%% Cell type:markdown id: tags:
4c. improve 4b
%% Cell type:code id: tags:
``` python
from tkinter import Tk, messagebox, ttk
# Function to capture and display text
def show_input():
user_input = entry.get()
messagebox.showinfo("Your Input", f"You entered: {user_input}")
# Create main window
wndw = Tk()
wndw.title("Enhanced GUI")
wndw.geometry("400x200")
# Create main frame
main_frame = ttk.Frame(wndw, padding=20)
main_frame.pack(expand=True)
# Add label
ttk.Label(main_frame, text="Enter something and click SEARCH:").pack(pady=5)
# Add text entry
entry = ttk.Entry(main_frame, width=30)
entry.pack(pady=5)
# Add button
ttk.Button(main_frame, text="SEARCH", command=show_input).pack(pady=5)
# Start the event loop
wndw.mainloop()
```
%% Cell type:markdown id: tags:
4d, e
%% Cell type:code id: tags:
``` python
from tkinter import Tk, messagebox, ttk
# Function to capture username and password
def login():
username = username_entry.get()
password = password_entry.get()
messagebox.showinfo("Login Info", f"Username: {username}\nPassword: {password}")
# Create main window
wndw = Tk()
wndw.title("Login Form")
wndw.geometry("300x200")
# Create frame
frm = ttk.Frame(wndw, padding=20)
frm.pack(expand=True)
# Username field
ttk.Label(frm, text="Username:").pack(pady=5)
username_entry = ttk.Entry(frm, width=25)
username_entry.pack(pady=5)
# Password field (hide input with "show='*'")
ttk.Label(frm, text="Password:").pack(pady=5)
password_entry = ttk.Entry(frm, width=25, show='*')
password_entry.pack(pady=5)
# Login button
ttk.Button(frm, text="LOGIN", command=login).pack(pady=10)
# Start the event loop
wndw.mainloop()
```
%% Cell type:markdown id: tags:
4f
%% Cell type:code id: tags:
``` python
from tkinter import Tk, messagebox, ttk
class LoginApp:
def __init__(self, root):
self.root = root
self.root.title("Login Form")
self.root.geometry("300x200")
# Create frame
frm = ttk.Frame(root, padding=20)
frm.pack(expand=True)
# Username field
ttk.Label(frm, text="Username:").pack(pady=5)
self.username_entry = ttk.Entry(frm, width=25)
self.username_entry.pack(pady=5)
# Password field
ttk.Label(frm, text="Password:").pack(pady=5)
self.password_entry = ttk.Entry(frm, width=25, show='*')
self.password_entry.pack(pady=5)
# Login button
ttk.Button(frm, text="LOGIN", command=self.login).pack(pady=10)
# Login function
def login(self):
username = self.username_entry.get()
password = self.password_entry.get()
messagebox.showinfo("Login Info", f"Username: {username}\nPassword: {password}")
# Create main window and run the app
if __name__ == "__main__":
root = Tk()
app = LoginApp(root)
root.mainloop()
```
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment