Modifying a Part of Text on tk.Button - python

I wonder if it is possible to change the weight of one word on a tkinter button? So the result would look something like this:
[ yes, I agree ]
I've tried using tags but neither tk.Button nor tk.Button['text'] seem to allow it.
Thanks!

No, it's not possible. If you want a button with rich text you'll have to create your own. Or, create an image that has the look you want, and use the image with a standard button.
You can create your own using a text widget that is one character tall and a few characters wide. Then, you can place bindings on the button to handle clicks, and to change the relief to simulate a button. Unfortunately, it won't have the look of the platform-specific buttons.

Related

How can I create a read-only text widget with TkInter and still be able to modify the widget's content programmatically?

I want to use Tk's text widget to display part of the state of my program. (A label widget will not do, because the tag feature of the text widget will save a lot of work. A canvas widget will not do, because I don't want to have to lay out a lot of text manually.)
I do not want the user to be able to directly modify the contents of the text widget. They can change the state of the program by interacting with it in other ways, but the text widget is for display only.
If I set the state of the text widget to disabled, then not only is the user unable to interact with it, but I also cannot modify its contents programmatically (specifically, I cannot insert text).
The obvious workaround is decorate any code that updates the contents of the text widget with code the enables and disables the widget. But this is kludgy: I should be able to modify the contents without offering the user an opportunity to interfere, however brief that opportunity may be.
Is there a way to do this?
The "kludgy workaround" isn't kludgy at all -- that's exactly how you do it, and how it was designed to work.
However, there is another solution. You can remove all of the built-in key and mouse bindings from the widget, and then re-implement only the ones you care about (for example, you might want the user to highlight and copy a block of text). This is simple and effective, but if you want to restore some of the bindings it starts to become very tedious to re-implement the bindings you care about (cut, copy, paste, page up, page down, moving the cursor, etc).
To remove all of the default bindings you can remove the class bind tag like this:
t.bindtags((t, root, "all"))
For more information about bind tags, see these answers:
https://stackoverflow.com/a/11542200/7432
https://stackoverflow.com/a/3513906/7432
https://stackoverflow.com/a/11459001/7432
you could constantly delete then insert the text like so:
import tkinter
root = tkinter.Tk()
text = tkinter.Text(root, width=40)
text.grid(row=0, column=0, stciky=tkinter.W)
while True:
text.delete(0.0, tkinter.END)
text.insert(tkinter.END, your_text)
where your_text is the text you want to insert

Better Method of Creating Multi Line Text Entry?

I need to create a message box for a user in a GUI with tkinter. For 3/4 of my entries, Entry(master, options. . .,) works. But for a message box, I need a multi line entry.
How would I do this? I tried ScrolledText(root).pack(), but it doesn't have the same commands/variables as Entry.
It is not explicitly mentioned in the documentation, but even if the tkinter.Entry widget's content can be scrolled, it can only be scrolled horizontally meaning that you can not use the yscrollcommandoption unlike with Canvas, Text and Listbox widgets.
This means technically your goal is not feasible, I mean you can not write multiple lines inside an Entry widget so that you scroll them vertically but only horizontally:
(if you need the code of this screenshot, please let me know).
Billal is right, however i would recomend simply using a Textbox.
go to: http://www.tutorialspoint.com/python/tk_text.htm
for more information

Python Tkinter: multiple images and text on a BIG button?

Is it possible to mount more than one image AND text on a Tkinter button?
Or, is it possible to put a FRAME containing images + text on a button?
I want a big button containing multiple widgets that, when taken together, fully describe the option the user will be able to choose.
I appreciate any suggestions!!
Is it possible to mount more than one image AND text on a Tkinter button?
Strictly speaking, no, it is not possible.
Or, is it possible to put a FRAME containing images + text on a button?
Yes, though it probably won't work on OSX. It would probably take you less time to actually try it than to type in the question on stackoverflow. A little research goes a long way.
You can also simply not use a button. Just use a frame or canvas, and set up bindings on the container and/or it's contents to react to a button click.

Gtk HeaderBar doesn't expand children

I'm using Gtk to build an application on Linux using Python 3. I'm trying to use a Gtk.HeaderBar. So far it's been working Ok, but it seems that I can't get it to expand it's child widgets. For example:
As you can see above, I've tried putting my Gtk.Entry into the Gtk.HeaderBar, but even with things like Gtk.Entry.set_hexpand(True) it simply refuses to expand. I've even tried putting it inside a Gtk.Box, expanding the Gtk.Box, then adding the Gtk.Entry inside that. Even when I set the Gtk.Entry as a custom title for the Gtk.HeaderBar, this happens:
What's causing this? How can I fix it?
Enabling hexpand only says that you want the widget to be allocated all the remaining space; it does not actually resize your widget. You want the halign property set to GTK_ALIGN_FILL (or whatever it's called in Python) in addition to hexpand.
Check the diagrams on this page for a visual explanation.
You can use Box instead of Headerbar with window.set_titlebar method

Tkinter: Listbox separators, disabled items, keyboard navigation?

I'm studying the Tkinter Listbox widget and have been unable to find solutions for the following functionality:
How can I create non-selectable horizontal separator items, eg. separators equivalent to the Tkinter Menu widget's .add_separator()? (Using chars like dashes and underscores looks awful).
How can I disable a specific item? I tried using .itemconfig( index, state='disabled' ) without success.
How can I enable keyboard navigation, eg. when a user's keyboard input automatically scrolls one forward to the closest item that begins with the text the user typed? Must I bind(<KeyPress>, ...) and manage this behavior myself?
Would some of the above functionality be easier to implement using a Text widget or the ttk.Treeview widget?
you cannot. The widget doesn't support that.
you can't disable certain items, the widget doesn't support a state attribute. That being said, you can monitor the selection and do the appropriate thing if the user selects something that is disabled, and use the item foreground to denote disabled-ness.
You will need to bind to keypress events and manage the behavior yourself. It's not particularly difficult, just a little tedious.
the text widget might be your best bet, though you'll have to add bindings to mimic the default bindings of the listbox.
Bottom line: Tkinter provides nothing that directly supports what you want to do, but the building blocks are all there. You'll just have to build it yourself.

Categories

Resources