help clearing text

Post here if you need help with creating a Graphical User Interface in Python.

help clearing text

Postby murster972 » Wed Aug 21, 2013 6:42 pm

I've made a basic program, to learn the basics of gui, and need some help.

heres the code
Code: Select all
from Tkinter import *

def txtoutput():
   mtext = storedtxt.get()
   mlabel2 = Label(root, text = mtext).pack()

def about():
   messagebx.showinfo(title = "About", message = "A very basic Program, to learn Gui")

def rb_1():
   rb_value = rb_var.get()
   rb_label = Label(root, text = "You select the option %s" % rb_value).pack()

root = Tk()
storedtxt = StringVar()
rb_var = IntVar()


mlabel = Label(root, text="Enter text and press button to output text...").pack()
mbutton = Button(root, text="Print text", command = txtoutput).pack()
entry = Entry(root, textvariable=storedtxt).pack()
radio_button = Radiobutton(root, text = "Option 1", variable = rb_var, value = 1, command = rb_1).pack()


when the button is pressed it displays the value stored in 'storedtxt' as a label under the entry box, but it dosnt delete after the label stays there, how would i get it to delete the label every time i pressed the button, so that there isnt lots of labels under the entry box??
Posts: 20
Joined: Fri Jul 19, 2013 8:54 pm

Re: help clearing text

Postby metulburr » Thu Aug 22, 2013 3:39 am

your creating the label each time in the callback function, thus it is creating a new label every time you push the button.

Here is a simplified version:
Code: Select all
    import tkinter as tk
except ImportError:
    import Tkinter as tk

root = tk.Tk()

e = tk.Entry(root)

s = tk.StringVar()

def callback():

tk.Button(root, text="get", command=callback).pack()
tk.Label(root, textvariable=s).pack()


and check out this regarding your star import
New Users, Read This
version Python 3.3.2 and 2.7.5, tkinter 8.5, pyqt 4.8.4, pygame 1.9.2 pre
OS Ubuntu 13.04, Mint 11, Arch Linux, Gentoo, Windows 7/8
User avatar
Posts: 1088
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: help clearing text

Postby murster972 » Thu Aug 22, 2013 6:56 pm

Thank you :D
Posts: 20
Joined: Fri Jul 19, 2013 8:54 pm

Return to GUI

Who is online

Users browsing this forum: No registered users and 1 guest