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 1 year ago.
Improve this question
When the button is pressed, I want the pictures in the selected folder path to be displayed at 1 second intervals. But the interface is not updated until the method I connected the button to finishes. What can I do?
def analyze_button_clicked(self):
for (root,dirs,files) in os.walk(self.input_output_tree.topLevelItem(0).text(0)):
for file in files:
if file.endswith(".jpg") or file.endswith(".jpeg"):
image_path = os.path.join(root, file)
pixmap = QPixmap(image_path)
self.process_image.setPixmap(pixmap)
It will likely update if you call QApplication.processEvents() after updating the Pixmap, like so:
from PyQt5.QtWidgets import *
def analyze_button_clicked(self):
for (root,dirs,files) in os.walk(self.input_output_tree.topLevelItem(0).text(0)):
for file in files:
if file.endswith(".jpg") or file.endswith(".jpeg"):
image_path = os.path.join(root, file)
pixmap = QPixmap(image_path)
self.process_image.setPixmap(pixmap)
QApplication.processEvents()
However, like Cristian said, it is advisable to use Signals & Slots
Related
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 1 year ago.
Improve this question
I am trying to get my widget to light up when the mouse is hovering over it. Here is what I have tried:
self.buttons = []
self.buttonTips = ["Select and Add a clip to the project", "Order the clips in your project", "Trim the selected clip",
"Cut out audio breaks in the selected clip", "Preview clip (in new window)", "Render/Export your video"]
self.commands = [self.getClipPath, self.reorderClips, self.trimClip, self.cutOutAudio, self._preview, self.finishVideo]
self.images = [_addImage, _switchImage, _trimImage, _autoTrimImage, _previewImage, _exportImage]
for index, tip in enumerate(self.buttonTips):
self.buttons.append(Button(self.root, image=self.images[index], command=self.commands[index], bg=_bgcolor, relief=FLAT))
self.buttons[index].photo = self.images[index]
self.buttons[index].place(x=index * 30, y=490)
self.buttons[index].bind("<Enter>", func=partial(changeButtonBG, 1, self.buttons[index]))
self.buttons[index].bind("<Leave>", func=partial(changeButtonBG, 0, self.buttons[index]))
addToolTip(self.buttons[index], tip)
When I change the event type to <Motion> the function runs perfectly, but when I use or it doesn't work? Anyone know why?
Ok I have figured out my problem. In this line here:
addToolTip(self.buttons[index], tip)
the addToolTip() function also adds a bind to the widget. So I edited the binds so they had:
button.bind(func=function, add="+")
which adding the add="+" made it work
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 2 years ago.
Improve this question
How would you code a button, which changes its Image after you press it, but after 3 Seconds it changes Back to its normal state ?
I managed to change the Button Image with a Click, but the function sleep() stops the whole Tkinter Mainloop, which leads to the Button not doing anything.
As mentioned above, you need to make another function which changes the image back. You would then schedule that function to run later using the after method. This would be an ideal place to use a Button subclass to make a custom type of Button that incorporates those functions and the image data. Like this:
import tkinter as tk
class EleckTroniiKz(tk.Button):
"""A new type of Button that shows one image, changes to another image
when clicked, and changes back to the original image after 3 seconds"""
def __init__(self, master=None, image1=None, image2=None, **kwargs):
self.command = kwargs.pop('command', None)
super().__init__(master, **kwargs)
self.image1 = tk.PhotoImage(file=image1)
self.image2 = tk.PhotoImage(file=image2)
self.config(image=self.image1, command=self.on_click)
def on_click(self):
self.config(image=self.image2)
if self.command: self.command()
# schedule the after_click function to run 3 seconds from now
self.after(3000, self.after_click)
def after_click(self):
self.config(image=self.image1)
### test / demo
def main():
root = tk.Tk()
btn = EleckTroniiKz(root, 'OFF.gif', 'ON.gif')
btn.pack()
root.mainloop()
if __name__ == '__main__':
main()
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 have been given a project in python and I'm not too familiar with it but I understand the basics due other coding work I've done.
The task is to basically make the application launch in last location it was closed.
There is no save button and no config (xml / ini etc.) currently used so I would assume it would need to create one of these, if not existing, or update an existing one upon closure of the window.
Everyone has different size monitors / monitor layouts and resolutions so is there a module that can assess this and save the X,Y co-ords per user configuration?
Or is there a better way to do this using the modules listed below? Or do I need to import an additional module?
These are the current modules imported:
import os
import sys
import pygtk
pygtk.require('2.0')
import gtk
Any help would be greatly appreciated.
UPDATE:
Forgot to post an update:
I managed to get this working perfectly into the existing app using J Arun Mani method.
I used os module to define the local directory where the file should be written to / read from and boom it works flawless. Thanks again
You can use the methods, Gtk.Window.get_position and Gtk.Window.move to get and set coordinates of window. (Link to doc)
But be aware that the placement of windows is users' preference and is taken care by window manager. So normally, you should not mess with it.
A simple example to demonstrate what you wanted:
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
def window_start(win):
try:
fp = open("win_coords")
except FileNotFoundError:
print("No file !")
return
coords = fp.read()
x, y = eval(coords) # Don't use eval if you can't trust the file
print("Moving to ", coords)
win.move(x, y)
fp.close()
def window_close(win, event):
fp = open("win_coords", "w")
coords = tuple(win.get_position())
print("Writing coordinates ", coords)
fp.write(str(coords))
fp.close()
win.destroy()
Gtk.main_quit()
label = Gtk.Label(label="Hello world")
win = Gtk.Window()
win.add(label)
win.connect("delete-event", window_close) # Connect to get the coordinates
win.show_all()
window_start(win)
Gtk.main()
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 3 years ago.
Improve this question
I have just started to use kivy. Currently, I am watching videos on kivy and copying their code. I copied it but got an error while the dude in the video didnt. I am trying to make a moving Label and a textinput box.
This is my code:
b = BoxLayout()
t = TextInput
f = FloatLayout()
s = Scatter()
l = Label(text="hell0")
f.add_widget(s)
s.add_widget(l)
b.add_widget(f)
b.add_widget(t)
this is the error im getting:
TypeError: descriptor 'fbind' requires a 'kivy._event.EventDispatcher' object but received a 'str'
You are doing a t = TextInput which does not create a TextInput widget, so your b.add_widget(t) fails because t is not a widget. Just change t = TextInput to t = TextInput(). And if you are using GridLayout, you must specify either cols or rows in the call to GridLayout().
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]