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()
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 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 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
#writing introduction text
mn=tk.Label(window,text="WELCOME TO \n AAROGYA SETU \n SELF EXAMINATION FRONT",font=("Bookman old style",30,'bold','italic')).pack()
#linking sign up form
sig=tk.Label(window,text="click button to sign up",font=("Bookman old style",20,"bold")).pack()
Error message:
'Nonetype' has no attribute .grid()
I am unable to position my label (that I created using Tkinter) in my program. it doesn't take .grid() or .place() either.
What can be the possible reasons? my code execution otherwise is perfect.
You do the most common mistake
variable = Widget().pack()
which assign None to variable because pack()/grid()/place() returns None.
You have to do it in two steps:
variable = Widget()
variable.pack()
But it seems you may create other problem.
pack()/grid()/place() are different layout managers and you shouldn't use two layout managers on one widget.
So use pack() OR grid() OR place().
You don't need pack() if you want to use grid() or place().
variable = Widget()
variable.grid()
or
variable = Widget()
variable.place(...)
or
variable = Widget()
variable.pack()
from tkinter import *
main = Tk()
name = Label(main,text="WELCOME TO \n AAROGYA SETU \n SELF EXAMINATION FRONT",font=("Bookman old style",30,'bold','italic'),bg="light blue").pack()
#linking sign up form
sign=Label(main,text="click button to sign up",font=("Bookman old style",20,"bold"),bg="light blue").pack()
main.mainloop()
this should work
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 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 7 years ago.
Improve this question
I have a window. There is a button. When the user clicks on this button, in the window must disappear button and appear new label and new button.
Do you understand?
# -*- coding: utf-8 -*-
from PyQt4 import QtGui, QtCore
import sys
def starting():
quest1 = QtGui.QWidget()
quest1.setWindowTitle('New')
quest1.resize(900, 600)
quest1.show()
quest1.exec()
testing = QtGui.QApplication(sys.argv)
window = QtGui.QWidget()
window.setWindowTitle('Title')
window.resize(900, 600)
MainText = QtGui.QLabel('<p align="center"; style="font-size: xx-large">Text</p>')
Mainbox = QtGui.QVBoxLayout()
# buttons
start = QtGui.QPushButton('Start')
quit = QtGui.QPushButton('Exit')
start.setFixedSize(70, 40)
quit.setFixedSize(70, 40)
buttons = QtGui.QHBoxLayout()
buttons.addWidget(start)
buttons.addWidget(quit)
# /buttons
Mainbox.addWidget(MainText)
Mainbox.addLayout(buttons)
window.setLayout(Mainbox)
QtCore.QObject.connect(quit, QtCore.SIGNAL('clicked()'), QtGui.qApp, QtCore.SLOT("quit()"))
QtCore.QObject.connect(start, QtCore.SIGNAL('clicked()'), starting)
window.show()
sys.exit(testing.exec_())
I think, I do wrong, is not required to make new window, but I don`t know what I must doing.
Qt UIs are built from widgets. Many widgets can have children. If you add/remove children, the UI will update accordingly.
The problem with the code above is that you don't add the new widget to a parent. So what happens is: You create the widget, you force it to appear, the function ends, the local variables (newwindow) end up on the trash and Python cleans the trash -> the widget is deleted again.
For the window to stay, you need to add it to some parent widget (probably the window). If you want to replace existing widgets, you need to remove them yourself.