IPython.display not showing TextBox in ipywidgets - python-3.x

I'm trying to build such a functionality such that whenever the user clicks in the Add button in my code, it generates a new text box, just under the old one. For example, like this:
Now, if the user were to click on add once again, a 5th text box should appear.
I've tried to achieve the same using this piece of code:
add_button = widgets.Button(description='Add',
disabled=False,
button_style='',
style={'description_width': 'initial', 'button_width': 'auto'},
icon='plus'
)
display(add_button)
add_button.on_click(add_new)
And my add_new function is simply defined as follows:
def add_new(*args):
display(widgets.Text(placeholder='Type something',description='String:'))
But this does not seem to be working nothing happens on clicking the button, any help would be appreciated. Also if there is a better way to do this, please help, I'm new to ipywidgets.

Try like this:
output = widgets.Output()
def add_new(*args):
with output:
display(widgets.Text(placeholder='Type something',description='String:'))
add_button = widgets.Button(description='Add',
disabled=False,
button_style='',
style={'description_width': 'initial', 'button_width': 'auto'},
icon='plus'
)
display(add_button)
add_button.on_click(add_new)
output

Related

Listbox resizing itself when new data is added

I am trying to add information to my Listbox and keeping it the size I state when I configure it. Here is my code for the Listbox with the scrollbar and an image of what it looks like.
Picture of the listbox.
taskList = Listbox(setBox, bg="#1B2834",fg="white")
taskList.configure(width=183,height=39)
taskList.pack(side=LEFT,fill=BOTH)
taskScroll = Scrollbar(setBox)
taskScroll.configure(bg="#1B2834",width=18)
taskScroll.pack(side = RIGHT, fill = BOTH)
taskList.config(yscrollcommand = taskScroll.set)
taskScroll.config(command = taskList.yview)
Now, when i click a button the command is to execute this following code:
def savetasks():
#make tasks
letters = string.ascii_uppercase
result_str = ''.join(random.choice(letters) for i in range(4))
num = str(random.randrange(0,9))
taskIDnum = num+result_str
taskIDLBL = Label(taskList, text=taskIDnum,bg="#1B2834", fg="White")
taskIDLBL.configure(padx=20,pady=10)
taskIDLBL.pack()
This code works fine as well, creating new labels with a random ID but it resizes the listbox to look like this...
Picture of the list box after clicking the button to execute the command.
Lastly, the scroll bar is not scrollable and when I create a lot of id's that end up going off my screen I cannot use the scroll bar to scroll down to see them, is there a way to not let the Listbox be resized and is it possible to set the Listbox with max and min-height?
If there is an easier way to do this without using a Listbox please let know, I just need to able to scroll down to see all the other id's and I didn't see any other way to use a scroll bar, that I NEEDED to use a Listbox

A problem for selecting a image from the imagepupop dialog

One more question. If I create a image-popup dialog, I find it only works when the frontimage (the top one in the image list). If other image is selected, the program will report "the image used in the expression does not exist". I can not understand the logic behind this error.
The following is a modified code pasted in the answer of the previous question. It can work well if the first image is selected, but the error message appears if the second image is selected.
I use GSM 2.30.xxxx
Class CMyDLG : UIframe
{
TagGroup DLG,DLGItems,imgPop
object Init(object self)
{
DLG = DLGCreateDialog("Test",DLGItems)
imgPop = DLGCreateImagePopup()
DLGItems.DLGAddElement( imgPop )
return self.super.init(DLG)
}
image GetSelectedImage( object self )
{
string selectedImageLabel
imgPop.DLGGetValue(selectedImageLabel) //DLGGetValue can return the label of the image diretly
Result("\n" + selectedImageLabel)
// From the string, get the label
//string label = selectedImageLabel.left( selectedImageLabel.find(":") )
//Result("\n" + label)
// From label, return image
//return FindImageByLabel(label)
return FindImageByLabel(selectedImageLabel)
}
}
// main
{
object dlg = Alloc(CMyDLG).Init()
dlg.Pose()
image selected = dlg.GetSelectedImage()
if ( selected.ImageIsValid() )
{
selected.SetName( "Selected" + random())
selected.ShowImage()
}
else Throw( "Error, nothing selected." )
}
Using the test code on GMS 3.3 it works except for the bug mentioned. I presume it's the same for GMS 2.3 but I haven't verified.
To make sure we test the same thing, here are exact instructions and a break-down:
Start out with two images A and B and A being front-most.
Run script
Don't change anything in the dialog
Press OK
ERROR
The dialog - taggroup does not (yet) hold any value. It possibly should, I consider this a bug.
Start out with two images A and B and A being front-most.
Run script
Click the selection box and select "A" from the drop-down
Press OK
A is correctly selected
Start out with two images A and B and A being front-most.
Run script
Click the selection box and select "B" from the drop-down
Press OK
ERROR
The dialog - taggroup does not (yet) hold any value. It definitly should, I consider this a bug. It is most likely what you were describing?
Start out with two images A and B and A being front-most.
Run script
Click the selection box and select "A" from the drop-down
Click the selection box and select "B" from the drop-down
Press OK
B is correctly selected
To summarize:
Yes, there is a bug and nothing wrong with your script.
The selection box only works after selecting an items for the second time.
The example code (first script) in this answer seems to work on any of the open images when selected.
However, there is the (mentioned) bug that it does not work on first selection, only when you select one image and then another.
If your code fails, please provided a slimmed-down code-example of the failing code so that a mistake can possibly be spotted.

how to create new inlinekeyboard when i click old inlinebutton, not replacing

i am new to python-telegram bot, I would like to create a new inline keyboard when i press the old inline keyboard without replacing the old one.
I used "editMessageText" to handle the callback query.but it only replaces the inlinekeyboard with "reply_markup",But I want to create a new inlinekeyboard.
How to solve this problem? I searched manytimes in stack overflow.But I couldnot find the solution still now?
Please help me to solve the problem!.My image is
This is my start(CommandHandler)
def start(bot, update):
bot.sendChatAction(update.message.chat_id, action=ChatAction.TYPING)
bot.send_message(chat_id=update.message.chat_id, text=Message.CLAIM,parse_mode='html')
reply_markup = telegram.InlineKeyboardMarkup([[telegram.InlineKeyboardButton("Check New Model",callback_data="New Model")],
[telegram.InlineKeyboardButton("Reasses my Insurance",callback_data="Reasses")],
[telegram.InlineKeyboardButton("File a Claim",callback_data="claim")]])
bot.sendMessage(chat_id=update.message.chat_id, text="Choose the above one?", reply_markup=reply_markup)
This is my Callback Handler
def callback(bot,update):
query=update.callback_query
if query.data=="claim":
reply_markup = telegram.InlineKeyboardMarkup([[telegram.InlineKeyboardButton("Vehicle",callback_data="vehicle")],
[telegram.InlineKeyboardButton("Personal Accident",callback_data="accident")],
[telegram.InlineKeyboardButton("Other",callback_data="other")]])
bot.editMessageText(
message_id = update.callback_query.message.message_id,
chat_id = update.callback_query.message.chat.id,
text = "Choose the one below",
reply_markup=reply_markup
)
As per it's name, bot.edit_message_text is for editing the text of the message. You need to use bot.edit_message_reply_markup (docs).
And if you want to add some buttons to the existing keyboard (if I understood your question), just include it in the edit:
reply_markup = telegram.InlineKeyboardMarkup([
[telegram.InlineKeyboardButton("Check New Model",callback_data="New Model")],
[telegram.InlineKeyboardButton("Reasses my Insurance",callback_data="Reasses")],
[telegram.InlineKeyboardButton("File a Claim",callback_data="claim")],[telegram.InlineKeyboardButton("Vehicle",callback_data="vehicle")],[telegram.InlineKeyboardButton("Personal Accident",callback_data="accident")],
[telegram.InlineKeyboardButton("Other",callback_data="other")]
])

choosing a specific button from a list of identical buttons

I am making a battleships program, and after i created a list of identical buttons for a grid and inserting them all into one list, i want to be able to choose the button just clicked and delete it. How can i achieve this?
l = []
for i in range (100):
b = Button(battleship.frame, height = 1, width = 3, command = )
l.append(b)
#this is a snippet of what i have now but i am not sure what to do.
I have tried using lambda to give each button something unique to make them all different but i don't think this helps me in selecting the specific button that was just clicked.
Any suggestions?

Python - Configuring multiple buttons at once in Tk

I have eighteen buttons that need to all change from one image to another upon the press of another button. I could just call .configure on each and set it that way, however, I feel as though there is a much cleaner simpler way. Any ideas?
If the buttons are all in a list, you can loop over them, like this:
self.buttons = [button1, button2, ..., button18]
def updateButtonImage(self):
for button in self.buttons:
button.configure(image=self.newImage)
updateButton = Button(root, text="Change button image", command=self.updateButton)
Is that what you had in mind?

Resources