I'm working on a bold button for a text editor. I'm having trouble with setting the button "sunken" when bold text is selected.
I imagine it will be something like the below?
self.textPad.tag_bind("bt","<Button-1>",self.boldP())
def boldP(self):
self.boldB.config(relief="sunken")
Not sure if I got the right idea, could someone please shed some light on this for me?
I'm not sure it's the main problem, but one problem with the code snippet that you posted is that you are not actually giving a callback to tag_bind, you are giving it None. Why? Because you are calling boldP, which returns None. Change this line:
self.textPad.tag_bind("bt","<Button-1>",self.boldP())
To:
self.textPad.tag_bind("bt", "<Button-1>", self.boldP)
Another problem is that your callback should take an event argument:
def boldP(self, event):
...
I'm not too familiar with Tkinter, so I'm not so sure about how tag_bind is supposed to work - but it seems like it's mostly used with canvas objects. Is this what you are doing? Otherwise try bind. Something like this:
self.button.bind(,"<Button-1>", self.boldP)
Related
I want to use KivyMDs FloatingActionButtonSpeedDial, with something different happening depending on which stack button is pressed. Right now, I'm just trying to print which button was pressed.
I've found outdated information from tutorials, but it looks like the current way to set a function on the button press is:
on_press_stack_button: root.callback(self)
then in my .py file I have
def callback(self, instance):
print(instance.icon)
but no matter which stack icon I choose, the result that's printed is always 'plus' (the root icon)
I'm guessing this is because I'm passing self into the callback function, but what else am I supposed to pass to access the actual button that's clicked? I can't find any examples in the docs, and all the tutorials I find say to use
'''
callback: callback
'''
instead of on_press_stack_button:
but using callback doesn't print anything at all.
Any help is greatly appreciated!
I need some help with the led option 'on_click_callback=' of the led widget in the module tk_tools.py. Here's what I have:
reactor_led = Led(win, size=50, toggle_on_click=True, on_click_callback=led_clicked(ON))
reactor_led.to_red()
reactor_led.to_green(on=True)
reactor_led.grid()
The led does get displayed properly in its window, and it does react correctly to a mouse click. I've omitted the code for led_clicked, but it is being called when I click the 'led'. The parameter "ON" is the boolean state of the led when clicked, but I can't come up with the actual code to reference that to pass to the call-back function. I need a working example to understand it.
Thanks
So I have this very simple thing I wrote and it's killing me trying to figure out why it won't work. All it does it print a statement when you click.
So for the first example I had a button and assigned the function printName1 directly to it, which worked perfectly fine.
Then the next thing was to bind it using the .bind() function. So in this case we just have a frame that prints out certain things based on which button you press. But unfortunately whenever I use bind, it throws the error show above. References tkinter\__init__.py for the error, so it's not something directly in my code but maybe it needs to be done differently? Thanks guys.
from tkinter import *
root = Tk()
def printName1():
print('Jack')
def printName2():
print('John')
def printName3():
print('Jill')
frame = Frame(root, width=300, height=250)
frame.bind("<Button-1>", printName1)
frame.bind("<Button-2>", printName2)
frame.bind("<Button-3>", printName3)
frame.pack()
root.mainloop()
EDIT: The error is confusing because it made it seem like there was an extra argument when there should be 0. But actually I needed to add an argument to the functions and that was event. so it should be def printName1(event) and so on. Just figured I would let you guys know what worked for me in case anyone stumbles upon this.
If you refer to the documentation regarding tkinter events and bindings, you will see that when an event is triggered, the associated event object will be passed as the first (and only) argument to the bounded function (being printName1 and friends in your case).
So what you need to do is to modify those printName* functions to accept the event argument.
def printName1(event):
print('Jack')
Then what you desired to achieve should work.
Naturally, you could make the event argument optional as #TigerhawkT3 suggested.
Events, such as from the keyboard/mouse, are all sent to the application with information about the event: which key was it, where was the mouse when you clicked, that sort of thing. This means that any callback bound to such an event needs to take an argument. If you want to also bind it to a Tkinter Button, which doesn't take an event, you can handle that as well. Just define your functions with a default argument:
def printName1(event=None):
...
I am trying to put a progress bar in a message box to display the progress of downloading of files; something like this:
(Image taken from here.)
So for the progress bar I will use this :
pbar=ttk.Progressbar(app,orient='horizontal',length=200,mode='determinate')
pbar.pack()
And maybe I could use a text widget, but I am not so sure how to make this new window appear. Do I write something like this?
root1=Tk()
root1.title("Status Dialog")
pbar=ttk.Progressbar(app,orient='horizontal',length=200,mode='determinate')
pbar.pack()
root1.mainloop()
Any ideas?
I think you have an error: in your code, pbar=ttk.ProgressBar(app, ...) should be replaced by pbar=ttk.ProgressBar(root1, ...). In this class instantiation, the first argument is the parent widget, and I bet it should be root1 in your code.
But as written by brc in comment, we can't be sure as long as you do not describe your error (give us the Traceback for example).
This is related to another question I found here that seems to be inactive for a few months, so I think it's worth asking again.
I have created a simple QDialog that has a QTextEdit and a QPushButton. This pops up in my application when a user right-clicks and selects the option to "add comments". I want them to be able to write free-form text and I'll just save whatever they write as a long string with no concern for new lines, etc.
When the user clicks the button, it executes code like this:
self.connect(accept_button,QtCore.SIGNAL('clicked()'),lambda arg=str(view_textedit.toPlainText()): self.updateGroupComments(arg))
def updateGroupComments(self,new_comment_str):
print "Updating user comment to have new string: " + new_comment_str
self.group_entry.list_of_user_comments[self.currentFrameCounter] = new_comment_str
This is not detecting the TextEdit text that is visible (it only detects whatever the text edit text is set to when it is created). How do I make a simple command that returns the currently visible text from a QTextEdit. Again, the function
toPlainText()
is not working correctly... it doesn't find the currently visible text, only whatever text was on screen before changes or additions started being made by the user.
If this can't be done without subclassing and appealing to cursor positions, it makes the whole thing seem worthless... so please keep suggestions only to those implemented without subclassing or manipulating cursors. It should be really simple and straightforward to just return all currently visible text... what am I missing?
Objects that are being bound to default arguments are evaluated at the definition time. The function is working correctly, it returns whatever was in the text field when it was executed. Your code simply calls it at the wrong moment. If you want to use lambda, then do:
self.connect(
accept_button, QtCore.SIGNAL('clicked()'),
lambda: self.updateGroupComments(str(view_textedit.toPlainText()))
)
Or make view_textedit an instance attribute instead, and do simply
self.connect(
accept_button, QtCore.SIGNAL('clicked()'), self.updateGroupComments
)
And change updateGroupComments to call self.view_textedit.toPlainText instead of taking an argument.
BTW, this is not PyQt specific, this is how Python works in general.
To illustrate my last comment, that lambda can very well be replaced with:
def slot():
self.updateGroupComments(str(view_textedit.toPlainText()))
self.connect(accept_button, QtCore.SIGNAL('clicked()'), slot)