Is there a way to make sound for each keys typing in an entry widget in tkinter?
Just a sample:
root = Tk()
e = Entry(root)
e.pack()
root.mainloop()
Thanks in advance :)
You could use event binding. Every time a key is pressed, it could play a sample sound.
In this example, it plays a very irritating beep sound. Since you use windows, I am using the winsound module.
Code:
from tkinter import *
import winsound
root = Tk()
e = Entry(root)
e.pack()
def key_pressed(event):
print("doing")
winsound.Beep(1000, 100)
e.bind_all("<Key>", key_pressed)
root.mainloop()
For non-windows users, you can use the playsound module. TO install it use the following:
python -m pip install playsound
Make sure to download a short beep sound to use in your program. Also, make sure to place both files in the same directory.
Code:
from tkinter import *
from playsound import playsound
root = Tk()
e = Entry(root)
e.pack()
def key_pressed(event):
playsound("Recording (52).wav")
e.bind_all("<Key>", key_pressed)
root.mainloop()
Hope this helps!
Related
i have a problem in my code in python (tkinter)
I have two files (F1.py) and (F2.py), each of which is a window, and I have another file called (main.py) that opens a window with two buttons, I want to click one of the (F1) files every time Or (F2) open.
What should I do ?
this is my code:
from tkinter import *
main = Tk()
def f1():
import f1.py
# I do not know what to do to run the file f1.py here !!
def f2():
import f2.py
# I do not know what to do to run the file f2.py here !!
btn_f2 = Button(main,text="open f2",command=f2).pack()
btn_f1 = Button(main,text="open f1",command=f1).pack()
mainloop()
Here is the work around according to your need, Since you want to execute your other tkinter window from your main python script. Here is the way to do that.
in your main.py
from tkinter import *
import os
import sys
import f1,f2
main= Tk()
def open(filename):
os.chdir("D:\\PYTHON_FILES\\") #change this path to your path where your f1.py and f2.py is located
# print("current dir "+os.getcwd())
os.system('python '+filename) #runnning the python command on cmd to execute both windows
btn_f2 = Button(main,text="open f2",command=lambda: open("f1.py")).pack()
btn_f1 = Button(main,text="open f1",command=lambda: open("f2.py")).pack()
main.mainloop()
Now define f1.py for example
from tkinter import *
window= Tk()
window.title('f1 Hello Python')
window.geometry("300x200+10+10")
mainloop()
Similarly for example we have f2.py
from tkinter import *
window= Tk()
window.title('f2 Hello Python')
window.geometry("300x200+10+10")
mainloop()
Now If you want to go for the legit way using TopLevel in Tinkter as #Matiis Described then here is clean and perfect way to achieve the goal.
Simply make a main.py as below
from tkinter import *
root = Tk()
root.geometry("200x200")
#here define your f1 window
def f1():
top = Toplevel(root)
top.geometry("400x400")
top.title("I am f1 window smaller than f2 but bigger than root")
top.mainloop()
#Similarly here define your f2 window
def f2():
top = Toplevel(root)
top.geometry("500x500")
top.title("I am f2 window bigger than f1")
top.mainloop()
btn1 = Button(root, text = "open f1", command = f1)
btn2 = Button(root, text = "open f2", command = f2)
btn1.place(x=75,y=50)
btn2.place(x=75,y=20)
root.mainloop()
I might be a little late with this, but I've recently started learning python and I had the same issue with tkinter. I had a "main.py" file and I wanted it to open "f1.py" by clicking a button.
I realized that when you configure a button in tkinter you have to assign a function so I wrote "main.py" as a regular pyhon/tkinter file importing f1.py:
from tkinter import *
from tkinter import ttk
import f1
main = Tk()
main.title("Main window")
but = Button(main, text="Open f1.py", command= f1.f1_func)
but.pack()
main.mainloop()
And instead of defining the function on the main file (def but_openwindow(): etc...) I wrote that function on the "f1.py" but JUST as a function, not as a mainloop. So the f1.py would be something like this:
from tkinter import *
from tkinter import ttk
def f1_func():
other = Toplevel()
other.title("Other window")
text = Label(other, text="This is another window")
text.pack()
And you could repeat it for any number of other .py files. So far I found out 2 important things:
1- I thought it wouldn't be necessary, but apparently you have to start f1.py importing tkinter libraries, otherwise it wouldn"t work. At least for me it didn't.
2- The files that you want to import can't have a "mainloop" or it just opens the other window as soon as you execute the main file.
I have yet to try to make an .exe of the main file, but I'm hoping to get there.
Sorry again for being late, hope it helps, if not for you, at leas for anyone else that might encounter this same problem.
I am building a program for Windows PCs that contains a lot of buttons and seems very plain. So I was wondering, can I make it so when you push a button (using tkinter), can I play a sound to liven up the program a bit? Please keep in mind I am learning so please dumb it down a bit.
Assuming your file is a WAV:
from tkinter import *
from winsound import *
root = Tk() # create tkinter window
play = lambda: PlaySound('Sound.wav', SND_FILENAME)
button = Button(root, text = 'Play', command = play)
button.pack()
root.mainloop()
Assuming your file is a MP3:
from Tkinter import *
import mp3play
root = Tk() # create tkinter window
f = mp3play.load('Sound.mp3'); play = lambda: f.play()
button = Button(root, text = 'Play', command = play)
button.pack()
root.mainloop()
You might want to consider using pygame as a cross-platform alternative to winsound.
import tkinter as tk
from pygame import mixer
mixer.init()
sound = mixer.Sound("sound.ogg")
root = tk.Tk()
tk.Button(root, command=sound.play).pack()
root.mainloop()
Refer to the docs for more information.
You first need to link the click of your mouse on the image, with an even handler, then simply define an on_click function:
def on_click(event):
winsound.Beep('frequency', 'duration')
Here you can find more information about playing sounds in python.
Just use
import os
os.system("play sound.mp3")
I am very noob and I need help please.
Here's my code:
from tkinter import *
root = Tk()
root.geometry('500x500')
def callback() :
print ("click!")
b = Button(root, text="OK", command=callback)
b.pack()
mainloop()
Try This:
from Tkinter import *
import tkMessageBox
root = Tk()
root.geometry('200x60')
root.title('Test Application')
def notify():
tkMessageBox.showwarning('Notification', 'Display is not capable of DPMS!')
b1 = Button(root, text="Run!", command=notify)
b1.pack()
root.mainloop()
Uri, you should probably run it on your computer instead of on the web. If you are using a chromebook go into settings and enable Linux.
——-Windows-——
Windows Install:
Click here
——-Mac-——
Mac Install:
click here
——-Linux-——
Linux Problems:
click here
Linux Install:
click here
I thought I would make a running clock program. I have this code which works for what I want it to do, but I want it to be fancy and output it to a new window. I thought of a message box but that would need constant closing.
Is there way around this, or should I just stick to using the console?
x=0
import datetime
import time
while x<10:
currentTime=datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
time.sleep(1)
print(str(currentTime))
EDIT:
This is what i have now but the window goes all over the place.
try:
from Tkinter import *
except ImportError:
from tkinter import *
import datetime
import time
x=0
while x<10:
root = Tk()
prompt = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
label1 = Label(root, text=prompt, width=len(prompt))
label1.pack()
def close_after_1s():
root.destroy()
root.after(1000, close_after_1s)
root.mainloop()
You're missing the point of Tk. The entire thing is a loop(hence the mainloop) and you keep destroying and creating a new window, hence the all over the place.
I think you just want something to update every sec:
from Tkinter import Tk,StringVar,Label
import datetime
def update():
global prompt,root
prompt.set(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
root.after(1000, update)
root = Tk()
prompt = StringVar()
label1 = Label(root, textvar=prompt, width=len(prompt.get()))
label1.pack()
update()
root.mainloop()
and my suggestion is to put all of this in a class. Google some Tk examples.
I am trying to use tkinter with ibPy. I am using Spyder (Spyder 2.3.0). When I enter the sample program
from tkinter import *
root = Tk()
w = Label(root, text="Hello, world!")
w.pack()
root.mainloop()
I receive the message:
File "/Users/Ameade/Tkinter.py", line 8, in <module>
from tkinter import *
ImportError: No module named tkinter
Do you know where I can get this module? I am working on a Mac (OSX 10.9.4).
It seems you named your sample program file Tkinter.py. You should change this name to something else and it should work.
EDIT
As Kevin said, give to the file any other name but not the name of a python module (the extension must remain .py), so you could name it my_amazing_program.py. And keep the content of the file the same as you originally posted it - if you're using python 3+:
from tkinter import *
root = Tk()
w = Label(root, text="Hello, world!")
w.pack()
root.mainloop()
If you're using python 2+ change tkinter to Tkinter on the import line:
from Tkinter import *
root = Tk()
w = Label(root, text="Hello, world!")
w.pack()
root.mainloop()
You may need to check the version of python you are using.
import tkinter works on python 3(instead of Tkinter)