Sujet : Re: Is Programming Obsolete?
De : ram (at) *nospam* zedat.fu-berlin.de (Stefan Ram)
Groupes : comp.miscDate : 07. Apr 2024, 19:51:32
Autres entêtes
Organisation : Stefan Ram
Message-ID : <rewrite-20240407194650@ram.dialup.fu-berlin.de>
References : 1 2
ram@zedat.fu-berlin.de (Stefan Ram) wrote or quoted:
import tkinter
tkinter.Label( text="Hi! What's your name?" ).pack()
entry=tkinter.Entry()
entry.pack()
button = tkinter.Button( text="Ok" )
def ok(): tkinter.Label( text="Hi, " + entry.get() + "!" ).pack()
button.pack()
button.config( command=ok )
tkinter.mainloop()
It can be rewritten to look a bit smaller and nicer:
from tkinter import *
Label( text="Hi! What's your name?" ).pack()
entry = Entry(); entry.pack()
def ok(): Label( text="Hi, " + entry.get() + "!" ).pack()
Button( text="Ok", command=ok ).pack()
mainloop()
Now let me ask the Chatbot!
|Chatbot, please write a tiny tkinter program to ask the user
|for his name and then greet him using his name.
Answer (The rest of my post is what the Chatbot answered):
|## Tiny Tkinter Program to Greet the User
|
|Here's a simple Tkinter program that asks the user for their
|name and then greets them:
|
|python
|import tkinter as tk
|
|def greet():
| name = name_entry.get()
| greeting_label.config(text=f"Hello, {name}!")
|
|root = tk.Tk()
|root.title("Greeting App")
|
|name_label = tk.Label(root, text="What's your name?")
|name_label.pack()
|
|name_entry = tk.Entry(root)
|name_entry.pack()
|
|greet_button = tk.Button(root, text="Greet Me", command=greet)
|greet_button.pack()
|
|greeting_label = tk.Label(root, text="")
|greeting_label.pack()
|
|root.mainloop()
|
|Here's how the program works:
|
|1. The `greet()` function is defined, which retrieves the
|user's name from the `name_entry` widget and updates the
|`greeting_label` with a personalized greeting.
|
|2. The main Tkinter window is created with the title "Greeting
|App".
|
|3. A label is added to prompt the user for their name.
|
|4. An entry widget is added to allow the user to input their
|name.
|
|5. A button is added with the text "Greet Me", which calls the
|`greet()` function when clicked.
|
|6. A label is added to display the greeting.
|
|7. The `mainloop()` function is called to start the Tkinter
|event loop and display the application.
|
|When the user enters their name and clicks the "Greet Me"
|button, the program will display a personalized greeting.