Home > database >  I'm trying to make something in a python gui where you type something into a input, click a but
I'm trying to make something in a python gui where you type something into a input, click a but

Time:06-08

so this is my code right now and I was looking to see if you could help me.

from tkinter import *
from tkinter import messagebox 

ad = Tk()
ad.geometry("300x300 500 200")
ed = Entry(ad)
ed.pack()

ed.focus_set()

def callback():
    pass

button = Button(ad, text = "OK", width = 10, command = callback)
button.pack()

mainloop()

CodePudding user response:

Frankly you should find it in many tutorials - ed.get()

You can use it to print() on screen or .append() to list.


import tkinter as tk  # PEP8: `import *` is not preferred

# --- functions ---

def callback():
    print( ed.get() )
    my_data.append( ed.get() )
    print(my_data)
    print('-----')
    
# --- main ---

my_data = []

root = tk.Tk()

ed = tk.Entry(root)
ed.pack()
ed.focus_set()

button = tk.Button(root, text="OK", command=callback)
button.pack()

root.mainloop()

PEP 8 -- Style Guide for Python Code

CodePudding user response:

from tkinter import *

ad = Tk()
ad.geometry("300x300 500 200")
ed = Entry(ad)
ed.pack()

ed.focus_set()

list = Listbox (ad)
list.pack()

def callback():
    text = ed.get()
    list.insert(END,text)

         

button = Button(ad, text = "OK", width = 10, command = callback)
button.pack()

mainloop()

CodePudding user response:

Try this. I just added the print format in the callback function. That's it.

from tkinter import *
from tkinter import messagebox 

ad = Tk()
ad.geometry("300x300 500 200")
ed = Entry(ad)
ed.pack()

ed.focus_set()

def callback():
    print(ed.get())

button = Button(ad, text = "OK", width = 10, command = callback)
button.pack()

mainloop()
  • Related