How to change Bitmap1 for ToolBarToolBase Object in wxPython - python

How does one change the "image" shown on a toolbar button dynamically in wxPython?
frame = wx.Frame( ... )
tb = frame.CreateToolBar()
tool_bmp = wx.Bitmap("/path/to/tool.png", wx.BITMAP_TYPE_PNG)
tb.AddLabelTool(id=wx.ID_ANY, label="Clicky", bitmap=tool_bmp, bmpDisabled=wx.NullBitmap, shortHelp="Clicky")
tbtb = tb.GetToolByPos(0)
Specifically, I want to change the "image" shown on the ToolBarToolBase object tbtb. I have tried things like:
new_bmp = wx.Bitmap("/path/to/new.png", wx.BITMAP_TYPE_PNG)
tbtb.SetBitmap1(new_bmp)
tb.Refresh()
and
tool_bmp = wx.BitMap("/path/to/new.png, sx.BITMAP_TYPE_PNG)
tb.Refresh()
to no avail.

Try using SetNormalBitmap instead
new_bmp = wx.Bitmap("/path/to/new.png", wx.BITMAP_TYPE_PNG)
tbtb.SetNormalBitmap(new_bmp)
tb.Realize()
tb.Refresh()

Related

HOW TO SET WINDOW BACKGROND IN URSINA ENGINE (PYTHON)

I can't set my game's window backgroun image.
This is what I have tried:
mainbg = Sky(texture = 'Assets/ruined_city_main_bg')
But that was incomprehensible and scary.
Also I have tried:
mainbg = Entity(parent = camera.ui, '''All other arguments''', texture = 'Assets/ruined_city_main_bg', position = (0, 0))
The "ENTITY BG" is not showing.
You can't see it because you didn't give it a model. Try adding model='quad'.
mainbg = Entity(parent=camera.ui, model='quad', texture='ruined_city_main_bg')
it looks like you are not setting the file extension.
For example:
ruined_city_main_bg.png
where .png is an image extension.
or try:
skybox_image = load_texture("sky_sunset.jpg")
Sky(texture=skybox_image)
Don't forget the file extension.

ipywidgets: button.on_click() has output delay

Introduction
I am trying to make a small tool for classifying images using the ipywidgets in a Jupyter Notebook, but I am having some trouble aligning the classes and the images. Do you have any suggestion how to fix this.
What I did
import ipywidgets as widgets
from IPython.display import display
import glob
# My images
image_paths = glob.glob("./images/*.png")
# Display image
def display_image(path):
file = open(path, "rb")
image = file.read()
return widgets.Image(
value=image,
format='png',
width=700,
height=700,
)
# Dropdown
def create_dropdown():
return widgets.Dropdown(
options=["1","2","3","4","5","6","7","8","9","10"],
value='5',
description='Category:',
disabled=False
)
# Creating widgets
input_dropdown = create_dropdown()
button = widgets.Button(description="Submit")
output_image = widgets.Image()
output_image.value = display_image(image_paths[-1]).value
# Define function to bind value of the input to the output variable
def bind_input_to_output(sender):
image_path = image_paths[-1]
image_score = input_dropdown.value
next_image_path = image_paths.pop()
print(image_score, image_path)
output_image.value = display_image(next_image_path).value
# Tell the text input widget to call bind_input_to_output() on submit
button.on_click(bind_input_to_output)
# Displaying widgets
display(output_image, input_dropdown, button)
Results
With the above code I end up categorising the upcoming picture, but I really don't understand why. It seems the widgets does not update the image the first time I press the button.
def bind_input_to_output(sender):
image_path = image_paths.pop()
image_score = input_dropdown.value
next_image_path = image_paths[-1]
print(image_score, image_path)
output_image.value = display_image(next_image_path).value
pop first and give next filename at last item

Opencv GUI python: arrange the created buttons

I use python 3.8 and Opencv in Linux.
I have several buttons that have stacked horizontally. How can I arrange them as I like (e.g., in a grid way?)
Is it possible to show some icons for each of the buttons?
Is it possible to make the fonts of the buttons bar larger?
Part of my script: (any suggestion to make my script better is appreciated)
if __name__== "__main__":
Folder_name = "male"
data_path = "path/to/images"
data_path = os.path.join(data_path, Folder_name)
all_imgs_path = glob.glob("{}/*.jpg".format(data_path))
all_imgs_path = sorted(all_imgs_path)
annot = annotation_tool(nof_imgs=len(all_imgs_path))
for index, im_dir in enumerate(all_imgs_path):
annot[index] = im_dir
item_path = "guid.jpg"
img = cv2.imread(item_path)
img_name = item_path.split("/")[-1]
cv2.imshow("{}".format(img_name), img)
cv2.createButton('Next', annot.Next, ["Next Image"])
cv2.createButton('Back', annot.Back, ["Previous Image"])
cv2.createButton('Submit', annot.Submit, ["Submit"])
# there are many of these buttons
UB_Tshirt = cv2.createButton("UB_Tshirt", annot.checkbox, "UB_Tshirt", 1, 0)
UB_Shirt = cv2.createButton("UB_Shirt", annot.checkbox, "UB_Shirt", 1, 0)
UB_Coat = cv2.createButton("UB_Coat", annot.checkbox, "UB_Coat", 1, 0)
cv2.waitKey(0)
print("end")
Edit:
As you see in the image, the buttons bar is very long and goes out of the screen. I would like to create a button pad that is squared.
This isn't a complete answer, but in regards to button arrangement, you have a little control using 'cv2.QT_NEW_BUTTONBAR'.
There's further detail here: https://docs.opencv.org/4.x/dc/d46/group__highgui__qt.html#gad15c7adb377e778dc907c0e318be193e

how to generate multiple buttons with a loop?

I have programmed software that displays a "tuile".
Definition of a tuile:
A tuile is a Frame which contains a button which displays an image and an explanatory text.
I would like to display 3 tuiles with 3 different settings.
listes_icones = ["icone1.png","icone2.png","icone3.png"]
listes_relx = [".3",".4",".5"]
listes_text = ["SYSTEM", "USER", "GAME"]
for i in range(3):
gen_img = PhotoImage(file=listes_icones[i])
gen_cadre = Frame(home,width=100, height=100,bg=bg_root)
gen_cadre.place(anchor="c", relx=listes_relx[i], rely=.5)
gen_img_bouton = Button(gen_cadre, image=gen_img, relief="flat",bg=bg_root)
gen_img_bouton.pack()
gen_text = Label(gen_cadre, text=listes_text[i], bg=bg_root, fg=text_color,font="blocktastic 18")
gen_text.pack()
I manage to display the text but not the button and the image, the variable is overwritten. How to solve this problem?
The problem that you are facing is like you said, the variable is overwritten in your loop. To solve this you need to keep track of your generated images. A simple solution is to store them in a list and get them in the next step. Here is an exampel:
import tkinter as tk
import PIL
listes_icones = ["icone1.png","icone2.png","icone3.png"]
gen_icons = []
listes_relx = [".3",".4",".5"]
listes_text = ["SYSTEM", "USER", "GAME"]
home = tk.Tk()
for i in range(3):
gen_img = tk.PhotoImage(file=listes_icones[i])
gen_icons.append(gen_img)
gen_cadre = tk.Frame(home,width=100, height=100)
gen_cadre.place(anchor="c", relx=listes_relx[i], rely=.5)
gen_img_bouton = tk.Button(gen_cadre, image=gen_icons[i], relief="flat")
gen_img_bouton.pack()
gen_text = tk.Label(gen_cadre, text=listes_text[i], font="blocktastic 18")
gen_text.pack()
home.mainloop()

Delete/hide HBox and children widgets using Button inside HBox

I am trying to make an input widget for a module i have made (see this SO question).
The input widget should have a title bar and a variable number of input lines below. I had in mind to have an delete button at the end of each input line.
The delete button should ideally delete the container widget and all the children widgets, but hiding the container widget and children would also be acceptable.
I have not been able to find a useful recipe to this problem.
Currently i got this code, but i have no clue less as how to solve the problem.
import ipywidgets as w
​
def add_btn_clicked(b):
input_box.children = (input_box.children[0], line()) + input_box.children[1:]
​
def delete_btn_clicked(b):
# ???
with output:
print(b.keys)
return None
add = w.Button(icon="plus-circle")
add.on_click(add_btn_clicked)
​
title = w.HBox([w.Label(value=str(i)) for i in range(3)]+[add])
​
def line():
delete = w.Button(icon="trash")
delete.on_click(delete_btn_clicked)
return w.HBox([w.FloatText(value=i) for i in range(3)]+[delete])
​
input_box = w.VBox([title,line()])
output = w.Output()
​
display(input_box)
display(output)
Is there a way to tell what the parent element is from the button click or another way to achieve what I am attempting?
You can create the widgets and container separately, then define the .parent attribute on the children as the container, before assembling together. That way you can effectively hide the container when the button is clicked (with .parent.layout.display = 'none').
import ipywidgets as w
def add_btn_clicked(b):
input_box.children = (input_box.children[0], line()) + input_box.children[1:]
def delete_btn_clicked(b):
b.parent.layout.display = 'none'
add = w.Button(icon="plus-circle")
add.on_click(add_btn_clicked)
title = w.HBox([w.Label(value=str(i)) for i in range(3)]+[add])
def line():
delete = w.Button(icon="trash")
delete.on_click(delete_btn_clicked)
val_widgets = [w.FloatText(value=i) for i in range(3)]
container = w.HBox()
delete.parent = container
for widg in val_widgets:
widg.parent = container
children = val_widgets + [delete]
container.children = children
return container
input_box = w.VBox([title,line()])
output = w.Output()
display(input_box)
display(output)

Categories