Tkinter - Changing Label with Function - function

I want to overwrite/replace the text Label with a button.
If I try to make a new label and place it at the same location, the previous text is still visable since it is longer than the new text.
Is it possible to delete the previous text first? any hints
root = tk.Tk()
tabControl = ttk.Notebook(root)
tab1 = ttk.Frame(tabControl)
def changetext():
labeltest = Label(tab1, text="short text").place(x=20, y=20)
labeltest = Label(tab1, text="long long long text").place(x=20, y=20)
button1 = Button(tab1, text="Change text pls", command=changetext)
button1.place(x=20, y=50)
root.mainloop()

Your code has this mistake. You shouldn't define a widget and pack/place/grid it on the same line. It's bad practise and you loose the reference to that widget. And as #acw1668 pointed out you can use <tkinter.Label>.config(text=<new text>) like this:
import tkinter as tk
def changetext():
labeltest.config(text="short text")
root = tk.Tk()
labeltest = tk.Label(root, text="long long long text")
labeltest.pack()
button1 = tk.Button(root, text="Change text pls", command=changetext)
button1.pack()
root.mainloop()

Related

Change C++ display text with HTML txt file?

I want to be able to change C++ display text from an HTML txt file, is this possible to do?
your issue is your using pack and grid. pack and grid do the same thing but grid lets you choose where to put it. you can only use one or the other in a canvas. also you had hello and goodbye in the same spot on grid(). heres your fixed code:
from tkinter import *
from tkinter import ttk
label = None
def change1():
global label
label.config(text="Hello World!")
def change2():
global label
label.config(text="Goodbye World!")
def main():
global label
rootWindow = Tk()
label = ttk.Label(rootWindow, text="Hello World!")
label.grid(row=0, column=0)
button1 = ttk.Button(rootWindow, text="Hello!", command=change1)
button1.grid(row=0, column=1)
button2 = ttk.Button(rootWindow, text="Bye!", command=change2)
button2.grid(row=0, column=2)
rootWindow.mainloop()
main()
your problem is that you are using both .grid() and .pack() the difference is that in .grid you can choose where to put your button or whatever while in .pack() it places it automatically. this is why I will recommend you to use the .grid() option.

Change button name on ipywidgets

Is there a way to easily change the button name in the ipywidgets module? I am using the decorator, but cannot find in the documentation how to change the name to something other than "Run Interact". I believe I need to use the decorator since my function needs to be run on demand and depends on multiple inputs from different widgets, but I'm open to other ways of doing so as well.
import ipywidgets as widgets
from IPython.display import display
#widgets.interact_manual(number1 = widgets.Dropdown(
options=[1,2],
description='select a number'),
number2 = widgets.Dropdown(
options=[3,4],
description='select another number'))
def add_numbers(number1,number2):
return number1+number2
A bit quick and dirty but you can set
widgets.interact_manual.opts['manual_name'] = 'Your text here'
before defining your function and this should change the label name. If you have multiple interact_manual calls that need different labels you will need to change it each time.
import ipywidgets as widgets
from IPython.display import display
widgets.interact_manual.opts['manual_name'] = 'Your text here'
#widgets.interact_manual(number1 = widgets.Dropdown(
options=[1,2],
description='select a number'),
number2 = widgets.Dropdown(
options=[3,4],
description='select another number'),)
def add_numbers(number1,number2):
return number1+number2

IPython notebook widgets using interactive

I'm having trouble creating widgets in a Jupyter notebook that update when other widget values are changed. This is the code I've been playing around with:
from ipywidgets import interact, interactive, fixed
import ipywidgets as widgets
from IPython.display import display
def func(arg1,arg2):
print arg1
print arg2
choice = widgets.ToggleButtons(description='Choice:',options=['A','B'])
display(choice)
metric = widgets.Dropdown(options=['mercury','venus','earth'],description='Planets:')
text = widgets.Text(description='Text:')
a = interactive(func,
arg1=metric,
arg2=text,
__manual=True)
def update(*args):
if choice.value == 'A':
metric = widgets.Dropdown(options=['mercury','venus','earth'],description='Planets:')
text = widgets.Text(description='Text:')
a.children = (metric,text)
else:
metric = widgets.Dropdown(options=['monday','tuesday','wednesday'],description='Days:')
text2 = widgets.Textarea(description='Text2:')
a.children = (metric,text2)
choice.observe(update,'value')
display(a)
The resulting widgets metric and text do change based whether A or B is selected, but the problem is that the "Run func" button goes away as soon as I change to B. I've tried adding the __manual attribute immediately before display(a), adding it within update, and several other places. How do I change the children of the widget box without overwriting the fact that I want to manually run the function?

AS3/Flash: to fill textinput fields with datagrid row data

I'd like to know if it's possible to fill text fields by clicking on a data grid row, or by selecting a row and clicking a button "fill text fields".
Must be in AS3/Flash.
Thanks in advance.
I did try several aproaches and find a solution:
// import fl.events.ListEvent; <----- Important
toPrint.myGrid.addEventListener(ListEvent.ITEM_CLICK, onClick);
function onClick(e:ListEvent):void
{
var over = e.item;
// Fill the dynamic text fields
name.text = over.Name;
surname.text = over.Surname;
company.text = over.Company;
year.text = over.Year;
}
WorkS like a charm.

dynamically change the color for every line in textarea and panel using flex 4 and action script 3

I have created a simple text chat application for android device using flex 4 and action script 3. in the text chat I have given a option for change font color. I have used "TextInput" for enter text message and "Textarea" for shows the chat messages. when I change the color of the text, all the lines in the "textarea" color has been changing.
textoutput.setStyle("color",textInput.getStyle("color"));
textoutput.text += userNameInput.text + ": " + msg + "\n";"
this is the code I have used. but I need to change the color for every line in the "textarea".
In the other hand, i have created the panel for display chat, and dynamically created the label for the message and dynamically I have changed the color but all the lines are merging in same line. I need to add line break for every dynamic label in the panel window.
var mylabel:Label=new Label();
mylabel.setStyle("color",textInput.getStyle("color"));
mylabel.text += userNameInput.text + ": " + msg + "\n";
panelId.addElement(mylabel);"
this is the code I have used for adding dynamic label into Panel . can any one kindly suggest me some idea for solving any one of this problem means that should be very helpful for me. thanks in advance.
Here is some info that might be helpful , from http://help.adobe.com/en_US/as3/dev/WS8d7bb3e8da6fb92f-20050207122bd5f80cb-7ff5.html
Formatting ranges of text within a text field
A useful method of the flash.text.TextField class is the setTextFormat() method. Using setTextFormat(), you can assign specific properties to the contents of a part of a text field to respond to user input, such as forms that need to remind users that certain entries are required or to change the emphasis of a subsection of a passage of text within a text field as a user selects parts of the text.
The following example uses TextField.setTextFormat() on a range of characters to change the appearance of part of the content of myTextField when the user clicks the text field:
var myTextField:TextField = new TextField();
myTextField.text = "No matter where you click on this text field the TEXT IN ALL CAPS changes format.";
myTextField.autoSize = TextFieldAutoSize.LEFT;
addChild(myTextField);
addEventListener(MouseEvent.CLICK, changeText);
var myformat:TextFormat = new TextFormat();
myformat.color = 0xFF0000;
myformat.size = 18;
myformat.underline = true;
function changeText(event:MouseEvent):void
{
myTextField.setTextFormat(myformat, 49, 65);
}