I'm woking on a project of a chatting app, using tkinter, running on python 3.7 and Windows 10 OS. One of the things I would like to add to my app, is an option of opening up the Windows's Touch Keyboard.
Although you can open the keyboard by pressing its button on the taskbar, I would like to give access to it from my app. My idea is to bind an Entry widget, used as the console line of my app, to an event, that whenever it occurrs, it makes the Touch Keyboard to pop up. The event I'll probably be using is '<FocusIn>', which means that the keyboard focus is moved to it. Here is a quick example of the mechanics:
def open_keyboard(event):
pass # open the Touch Keyboard
root = Tk()
console = Entry(root, font=('Verdana', 14), cursor='pencil', bg='red', fg='yellow') # creating console
console.pack()
console.bind('<FocusIn>', open_keyboard) # bind the console to the event
root.mainloop()
NOTICE: The Touch Keyboard IS NOT the On-Screen Keyboard. I don't want to use this keyboard, because it poppes up as a new window and not as a Toplevel window, which blocks my chatting app. More Importantly, it has no Emoji keyboard :)
A simple way to open the On-Screen Keyboard, is by running the following lines:
import os
os.system('osk')
I've been searching all over the internet for a solution, but they're all seem to be handling the On-Screen Keyboard. If someone knows how to help me or divert me to a source that explains how to handle it, he's more than welcomed to do so, because I'm stuck right now :/
Try this (explanation in code comment)
I think this directory will be the same on almost all Windows installations, still I'm looking for a way to make it path independent.
from tkinter import *
import os
root = Tk()
def callback(event):
# so the touch keyboard is called tabtip.exe and its located in C:\Program Files\Common Files\microsoft shared\ink
# here we run it after focus
os.system("C:\\PROGRA~1\\COMMON~1\\MICROS~1\\ink\\tabtip.exe")
frame = Frame(root, width=100, height=100)
frame.pack()
addressInput = Entry(frame, font = "Verdana 20 ", justify="center")
addressInput.bind("<FocusIn>", callback)
addressInput.pack()
root.mainloop()
Related
I've been trying to create a message box when a button turns from disabled to active but when the button actually turns from disabled to active for some reason my callback is not even being called I've tried to get it working so for quite a bit of time now and I'm stuck.
Here is an example of the problem:
from tkinter import *
from tkinter import Tk
def disable_and_activate():
b.config(state = DISABLED)
b.config(state = ACTIVE)
def is_working(event):
print('working')
root = Tk()
b = Button (root, text = 'click me', command = disable_and_activate)
b.pack()
b.bind('<Activate>', is_working)
root.mainloop()
Console:
the button is clicked but there's nothing printed on the console
The <Activate> event is not triggered when you set the state of the button to "active". The event is triggered when the window becomes the active window.
For example, when I run your code on my OSX machine, if I click on some other application to give it focus and then I click back to the tkinter window, the event will fire when the tkinter window becomes the active window.
This is explained in the canonical tcl/tk documentation which says this:
Activate, Deactivate
These two events are sent to every sub-window of a toplevel when they change state. In addition to the focus Window, the Macintosh platform and Windows platforms have a notion of an active window (which often has but is not required to have the focus). On the Macintosh, widgets in the active window have a different appearance than widgets in deactive windows. The Activate event is sent to all the sub-windows in a toplevel when it changes from being deactive to active. Likewise, the Deactive event is sent when the window's state changes from active to deactive. There are no useful percent substitutions you would make when binding to these events.
Here the problem was just the code inside the function , It seems like you needed to call EventGenerate('<<Activate>>') I also recommend adding 2 << and 2 >>
So I rewrote the code and its now working perfectly fine:
from tkinter import *
from tkinter import Tk
import tkinter
def disable_and_activate():
b.configure(state=tkinter.DISABLED)
b.configure(state=tkinter.ACTIVE)
b.event_generate("<<Activate>>")
def is_working(event):
print('working')
root = Tk()
b = Button (root, text = 'click me', command = disable_and_activate)
b.pack()
b.bind('<<Activate>>', is_working)
root.mainloop()
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
This is my first question.
I have a python program that recognize voice and reply with voice.
I wish to add a little GUI for my program (it should have only an image on background and a button to quit the program)
I would like that when I launch my code from terminal, it opened a Tkinter window and at the same time the python program start.
I’m working on Mac Os.
I use speech_recognition package to recognize voice and I use NSS speaker to let my computer speak.
This is a example of my code:
import speech_recognition as sr
from AppKit import NSSpeechSynthesizer
#VARIABLES
L = sr.Recognizer() #LISTENING
nssp = NSSpeechSynthesizer #SPEAKING
S = nssp.alloc().init()
while True:
audio = L.listen(source)
s = L.recognize_google(audio, language="en-US")
if s == "hi":
S.startSpeakingString_("Hello!!!")
Where do I have to write the Tkinter instructions to make sure that when I run my code it opens only a Tkinter window (while my program goes on) and not a shell's one?
You'll find it difficult to introduce your GUI as your code has already been written, note that everything in Tkinter has to be stored in some sort of Label or Widget and so you can't just print what you already have onto the Tkinter screen.
Here is some code to create a basic Tkinter window. Try searching online and playing around with how to present your variables within said window
import tkinter
from tkinter import *
root = tkinter.Tk()
root.configure(background = "#66ffdd") #here you can use any hex color code or just leave it blank and configure as default
root.title("Voice Program") #use the name of your program (this is the window header/title)
root.geometry("800x500") #these are your window dimensions
welcome = tkinter.Message(root, text = "Welcome to my program")
button = tkinter.Button(root, text="This button", command=print("hello")) #here insert a function for the button to perform i.e quit
welcome.pack()
button.pack() #packing presents your variables to the window - you can also use other geometry managers like grid
This site is really useful for showing you what widgets are available and what you can do with them - try searching any issues or posting a more specific question in the future if you struggle.
http://effbot.org/tkinterbook/button.htm
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()
#!/usr/bin/env python
# Display window with toDisplayText and timeOut of the window.
from Tkinter import *
def showNotification(notificationTimeout, textToDisplay):
## Create main window
root = Tk()
Button(root, text=textToDisplay, activebackground="white", bg="white", command=lambda: root.destroy()).pack(side=LEFT)
root.update_idletasks()
# Remove window decorations
root.overrideredirect(1)
timeOut = int(notificationTimeout*1000) # Convert to ms from s
## Run appliction
root.after(timeOut,root.destroy)
root.mainloop()
The above code creates a notification, with a timout. However on windows - the notification does not automatically pop up above all other present windows automatically. One has to click on the kill button (the text), and focus it the first time, after which the root window will be displayed above all other windows.
Is there a way to make the notification automatically appear above all other windows - on windows?
It seems to work on linux just fine (ubuntu 9.10).
According to this message you should be able to add the following after root.overridedirect(1). A quick test here suggests it should work for you.
root.wm_attributes("-topmost", 1)