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?
Related
I'm trying to write a program with ktinker with buttons in the top/bottom left and the top/bottom right. The issue is, I cannot put any buttons next to pre-existing buttons, only next to them, how would I do this?
#Welcome text and version number
Label(root, text="Wecome to %s" % banicVer).place(relx=0.5, rely=0.5, anchor=CENTER)
#Confirm button
#Button(root,text="Confirm\nand Save", command=root.destroy,).pack(side=BOTTOM,anchor=SE)
Button(root,text="Confirm\nand Save", command=root.destroy,).pack(side=BOTTOM,anchor=SE)
#Revert
#Button(root,text="Reset\nBootscreen", command=root.destroy,).pack(side=LEFT,anchor=SE)
Button(root,text="Reset\nBootscreen", command=root.destroy,).pack(side=BOTTOM,anchor=SE)
root.mainloop()
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
The "ok" button separates the two entry boxes from each other. I want them to stick together and the button making a square at the end of the two entries:
Here's the code
ventana = Tk()
ventana.geometry("500x300")
pathLabel = Label(ventana, text="Path of file: ").grid(row=0)
nameLabel = Label(text="Name of file").grid(row=1)
ePath = Entry()
eName = Entry()
ePath.grid(row=0, column=1)
eName.grid(row=1, column=1)
Ok = Button(text="okay", command=savepath)
Ok.grid(row=1, column=2, sticky=N)
Ok.configure(height=5)
ventana.mainloop()
This is how it looks
I want it to look like this
Thanks.
It sounds like what you want is for the button to be on row zero and extend to row one. So, that's exactly what you should tell grid.
If you want the button to exactly fit those two rows, I recommend not giving the button an explicit size. Instead, let grid make the button expand to fill the two rows by using the sticky option.
Ok.grid(row=0, column=2, rowspan=2, sticky="nsew")
I have been tinkering with Tkinter the last couple of days to create a little game.
I have searched for how to display text ontop of an image label but there is a little downside, it won't display exactly at the spot i am looking(in this case the top left side of the label).
I tried anchor and compound together in the same label but it doesn't seem to do anything new
the text still remains in the middle.
The code looks like this:
'''Adjusting the information frame'''
self.image_info = PhotoImage(file=locate_images("_info_panel", ""))
self.label_text_info = "• Welcome to Agony,\nto play or read the rules please go to File"
self.label_info = Label(self.info, text=self.label_text_info, font=10, image=self.image_info, compound=CENTER, anchor=NW)
self.label_info.grid(row=0, column=0)
Edit:
If I try to add anything else other than compound=CENTER the text is displayed outside the boundingbox of the image on the specified side.
Moved from an edit to the question by the OP to an answer.
Use a Canvas:
'''Adjusting the information frame'''
self.image_info = PhotoImage(file=locate_images("_info_panel", ""))
self.label_text_info = "• Welcome to Agony,\nto start the game or read the rules please go to File"
self.canvas_info = Canvas(self.info, width=300, height=600)
self.canvas_info.create_image((0, 0), image=self.image_info, anchor=NW)
self.canvas_info.create_text((140, 25), text=self.label_text_info)
self.canvas_info.grid(row=0, column=0)
I was approaching it wrong from the start, I am fearly new to tkinter
and I had to use a canvas to display the items inside of.
We're building a GUI interface with Python+tkinter.
The problem is when we want to set the view mode of an entity. I need to set the view mode or state of the treeview widget as 'disabled'.
How can we solve it?
Thanks for any support.
UPDATE
self.frmTab01.trvDetailorder.configure(selectmode='none')
I'm looking for a solution in which appart from disable the selection, affect the visualization of the widget just like an entry widget.
nbro is right, you need to change the Treeview style to make it look disabled. In addition, I also deactivated the possibility to open/close items when the Treeview is disabled using binding tricks on the mouse click.
In my example I added an entry so that you can compare the look on the two widgets. If you are using OS X or Windows, you might need to change the theme (style.theme_use("clam") should do) because their default themes are not very customizable.
from tkinter import Tk
from tkinter.ttk import Treeview, Style, Button, Entry
root = Tk()
def toggle_state():
if "disabled" in tree.state():
e.state(("!disabled",))
tree.state(("!disabled",))
# re-enable item opening on click
tree.unbind('<Button-1>')
else:
e.state(("disabled",))
tree.state(("disabled",))
# disable item opening on click
tree.bind('<Button-1>', lambda e: 'break')
style = Style(root)
# get disabled entry colors
disabled_bg = style.lookup("TEntry", "fieldbackground", ("disabled",))
disabled_fg = style.lookup("TEntry", "foreground", ("disabled",))
style.map("Treeview",
fieldbackground=[("disabled", disabled_bg)],
foreground=[("disabled", "gray")],
background=[("disabled", disabled_bg)])
e = Entry()
e.insert(0, "text")
e.pack()
tree = Treeview(root, selectmode='none')
tree.pack()
tree.insert("", 0, iid="1", text='1')
tree.insert("1", 0, iid='11', text='11')
Button(root, text="toggle", command=toggle_state).pack()
root.mainloop()