TKInter buttons change or destroy - python

I have an issue with accessing to Tkinter button objects.
My idea that 2nd button is disabled if first button wasn't pressed.
If 1st button is pressed TopLevel window pops up with input fields.
When 3rd button is pressed works my function.And I'd like to change text of 4th button and destroy 3rd button.
I can't do that from my function
self.win = tk.Toplevel()
self.win.geometry('350x200')
self.win.transient(root)
self.win.grab_set()
self.btn_cancel = tk.Button(master=self.win, text='Отмена',
command=self.win.destroy).grid(row=8, column=1)
self.btn_conn = tk.Button(master=self.win, text='Проверить',
command=self.entry_db_check).grid(row=8, column=0)
def entry_db_check(self):
<my code is here>
After all manipulation 2nd button must be active.
I saw that it should work so
self.btn_cancel.destroy()
self.btn_conn['text']='OK'
Should I switch to frames instead of TopLevel window to make it work?

Related

How to modify a selected text widget in tKinter when pressing a button?

I have a program that uses text and button widgets. I want it to insert a string in the text widget when I press the button, but I have no idea of how to do that. Could anyone help me?
Graphical user interfaces have the concept of keyboard focus (or focus for short). A widget with the focus will be the widget that gets keyboard events. Normally there can only be a single widget with the keyboard focus at any one time. For the most part, focus is handled automatically. For example, if you click in a text widget or entry widget, that widget will be given the focus.
The answer to your question is to call tkinter's focus_get method to get the widget with the keyboard focus. You can then call the insert method to insert text into that widget.
The following is a simple example. Click on any text widget and then click the button, and the string "Hello!" will be inserted in whichever text widget has the focus.
import tkinter as tk
def insert_hello():
widget = root.focus_get()
widget.insert("end", "Hello!")
root = tk.Tk()
button = tk.Button(root, text="Hello", command=insert_hello)
button.pack(side="top")
for i in range(3):
text = tk.Text(root, width=40, height=4)
text.pack(side="top", fill="both", expand=True)
root.mainloop()

Tkinter keyboard event dosen't work with characters

[Raspberri PI 3 b+, Python]
First of all, I would apologizr gor my language skill.
I am coding the Tkinter for keyboard event, The keyboard command button like (Up) (Down) is work well but the normal charactors dosen't work (Such as 1-9, A-Z)
I have tired
frame.bind('<Left>', leftKey) # THIS OK
frame.bind('<Right>', rightKey) # THIS OK
but
frame.bind('<1>', leftKey) # Not work
frame.bind('1', leftKey) # Not work
frame.bind("1", leftKey) # Not work
I would like to use keyboard charactor botton to works properly same as Up, Down button.
"1" and '1' should work. "<1>" represents mouse button 1.
If you are binding to a frame, you must make sure that it has keyboard focus. By default Frames do not have keyboard focus.
For example, to force the keyboard focus to a frame you need to call focus_set:
frame.focus_set()
This could happen if the frame doesn't have the focus so frame.bind('<1>', leftKey) will not work.
You can check which widget has the focus by print frame.focus_get().
There are two ways you can solve your problem.
Either you set focus to the frame before binding callback
Example:
from tkinter import *
root = Tk()
root.geometry('100x100+100+100')
frame = Frame(root)
frame.pack()
frame.focus_set() # This will get the frame in focus.
# If the frame is in focus the bind will work.
frame.bind( "1", lambda _: print(frame.focus_get()) )
root.mainloop()
Or
Just bind it to the main window.
from tkinter import *
root = Tk(). # Main window
# bind the callback to the main window.
root.bind( '1', lambda k: print(k) )
root.mainloop()

Closing a window in python 3.7.2 with tkinter

I've made a program in python with Tkinter that allows you to free draw and choose different colors. I decided to make a button that would close the window instead of clicking the exit button in the top right corner. My question is how do I make the window close when the button is pressed?
If you are using a main loop for your application, then you can use the .destroy() method to release all the resources associated with the window and close the application. You call this method within the command function for your button like so:
from tkinter import *
root = Tk()
frame = Frame(root)
frame.pack(side=LEFT)
button = Button(frame, text="Exit", command=exit)
button.pack()
root.mainloop()
def exit():
root.destroy()
That should close your window. Optionally, the destroy() method may also be used at the end of your main loop if the X button of your application won't close the window immediately.
See these examples for more info:
http://effbot.org/tkinterbook/widget.htm#Tkinter.Widget.destroy-method
http://effbot.org/tkinterbook/tkinter-hello-again.htm

Tkinter Button doesn´t change it´s relief after pressing it

Why does my tkinter Button stays in the "sunken" relief after I press it?
import tkinter
from tkinter import messagebox as msgbox
class GUI(object):
def __init__(self):
self.root = tkinter.Tk()
self.root.geometry("200x200")
self.root.title("Test")
self.testButton = tkinter.Button(self.root, text="Click Me!")
self.testButton.bind("<Button-1>", self.click)
self.testButton.bind("<ButtonRelease-1>", self.release)
self.testButton.pack()
def release(self, event):
event.widget.config(relief=tkinter.RAISED)
def click(self, event):
result = msgbox.askokcancel("Continue?", "Do you want to continue?")
if result:
print("Okay")
else:
print("Well then . . .")
print(event.widget.cget("relief"))
print()
if __name__ == "__main__":
test = GUI()
test.root.mainloop()
The console shows that the relief is "raised" but on the GUI it stays in the "sunken" relief , why?
The GUI after pressing the Button
Your callback is printing "raised" because your code is run before the default button bindings, so the button relief is in fact raised at the point in time when your function is called.
I'm pretty sure this is what is causing the button to stay sunken:
you click on the button, and a dialog appears. At this point the button is raised because tkinter's default binding has not yet had a chance to run 1, and it is the default bindings which cause the button to appear sunken
a dialog appears, which steals the focus from the main window.
you click and release the button to click on the dialog. Because the dialog has stolen the focus, this second release event is not passed to the button
at this point the processing of the original click continues, with control going to the default tkinter binding for a button click.
the default behavior causes the button to become sunken
at this point, your mouse button is not pressed down, so naturally you can't release the button. Because you can't release the button, the window never sees a release event.
Because the button never sees a button release event, the button stays sunken
1 For a description of how tkinter handles events, see this answer: https://stackoverflow.com/a/11542200/7432. The answer is focused on keyboard events, but the same mechanism applies to mouse buttons.

Run function from Python 2 Tkinter button when pressed then run another when released

Hello I am trying to make a simple recorder in Python 2.7 using Tkinter as the GUI, I want to be able to record when the button is pressed then save the recording when the button is released, I know how to make the button and have already done so, but I don't know how to make it run a program when pressed and another when released, is it possible?
Also I'm not sure how to actually record from the microphone and save it using pyaudio, any help with this is appreciated but I'm sure I can figure this out myself when I have overcome the main issue.
You can bind an event to the click of the left mouse button <Button-1> and to the release of the left mouse button <ButtonRelease-1>. Here's an example:
import Tkinter as tk
root = tk.Tk()
def clicked(event):
var.set('Clicked the button')
def released(event):
var.set('Released the button')
var = tk.StringVar()
var.set('Nothing to see here')
label = tk.Label(root, textvar=var)
label.pack()
but = tk.Button(root, text='Button')
but.bind("<Button-1>", clicked)
but.bind("<ButtonRelease-1>", released)
but.pack()
root.mainloop()

Categories

Resources