Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I need to play a typewriter key sample on every tkinter.text input.
I came across the Playsound module but I don't know how to listen to the inputs.
You would use a bind and setup a function to play the sound when the bind is triggered.
import tkinter as tk
def key(event):
print("pressed", repr(event.char))
# Play sound here
root = tk.Tk()
text = tk.Text(root)
text.pack()
text.bind('<Key>', key)
root.mainloop()
Thanks, I've come up with a very similar solution tho its really really laggy. Im basically writing a simple typewriter emulator so the key sound is reproduced for each typed letter.
import tkinter as tk
from PIL import Image, ImageTk
from playsound import playsound
def key(event):
key = event.char
playsound("C:/Users/Isma/key1.mp3")
win = tk.Tk()
frame = tk.Frame(win, width=300, height=400)
frame.grid(row=1, column=0)
text = tk.Text(frame)
text.grid(row=0,column=0)
text.bind('<Key>',lambda a : key(a))
image = Image.open("C:/Users/Isma/swintec1.jpg")
photo = ImageTk.PhotoImage(image)
label = tk.Label(frame,image=photo)
label.image = photo
label.grid(row=3,column=0)
win.mainloop()
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
Is there any way to show the message in popup window that process is running when python script start and disappear the popup window when process is done. Thanks in advance
A very flexible method you could look into is creating a tkinter window:
https://docs.python.org/3/library/tkinter.html
import tkinter as tk
import time
def some_function():
print('Do stuff')
time.sleep(3)
if __name__ == '__main__':
# Create window
window = tk.Tk()
# Create label
label_var = tk.StringVar()
label_var.set('Program is running...')
label = tk.Label(window, textvariable=label_var)
label.pack()
# Update and show window once
window.update_idletasks()
window.update()
# Your function code
some_function()
# Get rid of window
window.destroy()
Edit: just saw someone answered with tkinter in the comments... will leave this here as an example
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I would want to create a button with tkinter that when you click it then it changes colour. I konw that I can do it by deleting the button but is there any other possibilitis.
Well, there are a lot of options. You can use many GUI libraries like Kivy, Tkinter. But as i dont know what you are using, i am just gonna make a program in Tkinter.
here is the code -
# importing modules
from tkinter import *
import random
# making a window
root = Tk()
root.geometry("400x400")
# just for decoration or the Background Color
mainframe = Frame(root, bg="#121212", width=400, height=400)
mainframe.pack()
# the function changing color
def color_changing(buttonObject):
# randomizes the color in hexcode
r = lambda: random.randint(0, 255)
color = '#%02X%02X%02X' % (r(), r(), r())
# .config configures an Object in Tkinter
buttonObject.config(bg=color)
# making a button
button = Button(root, width=10, font=('Segoe UI', 32, "bold"), text="Click Me", command=lambda: color_changing(button))
# root in Button() is the place where the button has to be placed
# text, width, font, etc are all attributes
# command is a simple way to call a function
# placing it in a specific location
button.place(relx=0.5, rely=0.5, anchor=CENTER)
# making the window so it does not stop running till it is said to be
root.mainloop()
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
This is my current db.py file, it contains the tkinter code for creating the GUI:
import tkinter
import db
app = Tk()
app.geometry("450x300")
app.mainloop()
You can use a Entry widget with a linked variable to get the input from user. The content can then be retrieved from the variable and written to a file using file objects.
I like to use themed-tkinter(ttk). If you just starting with creating GUI, I suggest you read more about themed-tkinter here.
from tkinter import ttk
import tkinter as tk
root = tk.Tk()
# StringVar has all the logic to store and update string values
content = tk.StringVar()
def callback():
# This function is invoked when button `submit` is clicked
with open('content.txt', 'w') as file:
file.write(content.get())
entry = ttk.Entry(root, textvariable=content).grid()
submit = ttk.Button(root, text='submit', command=callback).grid()
root.mainloop()
Edit: I worded my answer wrongly for which I appologize. Tkinter by itself is indeed robust and powerful. I found it more easier to use ttk in many cases and had a softcorner towards it.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I'm trying to make some basic GUI and I'm having some trouble with this code:
with open(project_dir + 'logs/wash.log') as f:
for line in f.readlines():
if not line.startswith('BSSID ') and \
not line.startswith('------------'):
print(line)
At this point of the application I have the tkinter root already opened in the background and a terminal opened too, I would like to open a new tkinter window in which display the line printed above, I suppose adding a label in the window for each line that I need to display.
I tried tk.Toplevel() but I don't know how to make the new window in which display the strings.
My problem is I am trying to create a new window and print the strings there, I have tried tk.Toplevel() but I don't know how to make the new window in which to display the strings
From your comment:
my problem is to create a new window and print there the strings, I tried tk.Toplevel() but I don't know how to make the new window in which display the strings
This is a simple example but should help.
I have a button on the root window that links to a function called new_window(). This function will create a top level window containing a text box widget. We then use the with open statement to write the data to the text box.
import tkinter as tk
root = tk.Tk()
def new_window():
top = tk.Toplevel(root)
my_text_box = tk.Text(top)
my_text_box.pack()
with open(project_dir + 'logs/wash.log') as f:
for line in f.readlines():
if not line.startswith('BSSID ') and \
not line.startswith('------------'):
my_text_box.insert("end", line)
open_new_window = tk.Button(root, text="Open Toplevel", command=new_window)
open_new_window.pack()
root.mainloop()
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'd like to get all file names in a directory and present them to user in a listbox, then user may choose multiple names and press OK or Cancel. If he presses OK, it should return selected file names. Please help.
here is a fairly simple way using Tkinter:
from Tkinter import *
root = Tk()
opt_list = ['opt1','opt2','opt3','opt4','opt5']
sel_list = []
def get_sel():
sel_list.append(Lb1.curselection())
root.destroy()
def cancel():
root.destroy()
B = Button(root, text ="Submit", command = get_sel)
C = Button(root, text ="Cancel", command = cancel)
Lb1 = Listbox(root, selectmode=MULTIPLE)
for i,j in enumerate(opt_list):
Lb1.insert(i,j)
Lb1.pack()
B.pack()
C.pack()
root.mainloop()
for i in sel_list[0]:
print opt_list[int(i)]
then you can this to get the selected options:
for i in sel_list[0]:
print opt_list[int(i)]
this will create a listbox using the items from sel_list then when the user presses submit it will return a tuple of which lines are selected
multiple can be selected at a time and will returned in a tuple get more information from this site Python Tk Tutorials Point
More specifically, what you want is http://tkinter.unpythonic.net/wiki/tkFileDialog
#python 3
from tkinter.filedialog import askopenfilename
filenames = askopenfilename(multiple=True)
This returns a list of paths to the files the person chose, to extract the filename:
import os
filenames = [os.path.basename(filename) for filename in filenames]
and if you want the filename without extension, instead of the line above use:
filenames = [os.path.splitext(os.path.basename(filename))[0] for filename in filenames]