Popup / Message while python script is executing - python

I want to show a message(popup) while python script is executing and automatically disapper when the script done exectuing
import sys
from tkinter import ttk
from tkinter import messagebox
try:
import Tkinter as tk
except ImportError:
import tkinter as tk
try:
import ttk
py3 = False
except ImportError:
import tkinter.ttk as ttk
py3 = True
import new_support
def vp_start_gui():
'''Starting point when module is the main routine.'''
global val, w, root
root = tk.Tk()
top = Toplevel1 (root)
new_support.init(root, top)
root.mainloop()
w = None
def create_Toplevel1(root, *args, **kwargs):
'''Starting point when module is imported by another program.'''
global w, w_win, rt
rt = root
w = tk.Toplevel (root)
top = Toplevel1 (w)
new_support.init(w, top, *args, **kwargs)
return (w, top)
def destroy_Toplevel1():
global w
w.destroy()
w = None
class Toplevel1:
def smsdata(self):
MsgBox = tk.messagebox.showinfo('Analysing', 'Please wait till the data is being analysed')
* My Script*
def __init__(self, top=None):
'''This class configures and populates the toplevel window.
top is the toplevel containing window.'''
_bgcolor = '#d9d9d9' # X11 color: 'gray85'
_fgcolor = '#000000' # X11 color: 'black'
_compcolor = '#d9d9d9' # X11 color: 'gray85'
_ana1color = '#d9d9d9' # X11 color: 'gray85'
_ana2color = '#ececec' # Closest X11 color: 'gray92'
self.style = ttk.Style()
if sys.platform == "win32":
self.style.theme_use('winnative')
self.style.configure('.',background=_bgcolor)
self.style.configure('.',foreground=_fgcolor)
self.style.map('.',background=
[('selected', _compcolor), ('active',_ana2color)])
top.geometry("534x226+710+62")
top.minsize(1, 1)
top.maxsize(1905, 898)
top.resizable(1, 1)
top.title("SMS_Analysis")
top.configure(highlightbackground="#00d81d")
self.Frame1 = tk.Frame(top)
self.Frame1.place(relx=0.0, rely=0.0, relheight=0.996, relwidth=1.002)
self.Frame1.configure(relief='ridge')
self.Frame1.configure(borderwidth="2")
self.Frame1.configure(relief="ridge")
self.Frame1.configure(background="#180bd8")
self.Execute = tk.Button(self.Frame1)
self.Execute.place(relx=0.093, rely=0.089, height=104, width=436)
self.Execute.configure(activebackground="#10ed00")
self.Execute.configure(activeforeground="#ffffff")
self.Execute.configure(background="#10d802")
self.Execute.configure(relief="groove")
self.Execute.configure(text='''Execute''')
self.Execute.configure(command=self.smsdata);
self.Message1 = tk.Message(self.Frame1)
self.Message1.place(relx=0.224, rely=0.756, relheight=0.133
, relwidth=0.563)
self.Message1.configure(text='''Wait For 2 Min''')
self.Message1.configure(width=301)
self.TProgressbar1 = ttk.Progressbar(self.Frame1)
self.TProgressbar1.place(relx=0.093, rely=0.578, relwidth=0.804
, relheight=0.0, height=19)
self.TProgressbar1.configure(length="430")
if __name__ == '__main__':
vp_start_gui()
I want to display Popup While following code is running
def smsdata(self):
MsgBox = tk.messagebox.showinfo('Analysing', 'Please wait till the data is being analysed')
* My Script*
After execution of the popup should disappear.
I have created a MsgBox but the issue is when I press "OK" then script starts executing. I want the popup to appear when code is being executed and vanish when complete executing
new_support
import sys
try:
import Tkinter as tk
except ImportError:
import tkinter as tk
try:
import ttk
py3 = False
except ImportError:
import tkinter.ttk as ttk
py3 = True
def init(top, gui, *args, **kwargs):
global w, top_level, root
w = gui
top_level = top
root = top
def destroy_window():
# Function which closes the window.
global top_level
top_level.destroy()
top_level = None
if __name__ == '__main__':
import new
new.vp_start_gui()

You can use multithreading module or thread module if you want to process multiple commands at the same time if this is the answer that you are searching for.

Related

Pygame & Tkinter Musikplayer

Please can you help me I don't know what´s the problem.
This is the Error:
[Running] python -u "c:\Users\Leon\Desktop\Page Guis\Music Player Pro ++\unknown.py"
File "c:\Users\Leon\Desktop\Page Guis\Music Player Pro ++\unknown.py", line 49
mixer.music.play()
^
TabError: inconsistent use of tabs and spaces in indentation
[Done] exited with code=1 in 0.651 seconds
My code:
# -*- coding: utf-8 -*-
from pygame import mixer
import tkinter.filedialog
import sys
try:
import tkinter as tk
except ImportError:
import tkinter as tk
try:
import tkinter.ttk
py3 = False
except ImportError:
import tkinter.ttk as ttk
py3 = True
import unknown_support
mixer.init()
def vp_start_gui():
'''Starting point when module is the main routine.'''
global val, w, root
root = tk.Tk()
top = Toplevel1 (root)
unknown_support.init(root, top)
root.mainloop()
w = None
def create_Toplevel1(rt, *args, **kwargs):
'''Starting point when module is imported by another module.
Correct form of call: 'create_Toplevel1(root, *args, **kwargs)' .'''
global w, w_win, root
#rt = root
root = rt
w = tk.Toplevel (root)
top = Toplevel1 (w)
unknown_support.init(w, top, *args, **kwargs)
return (w, top)
def destroy_Toplevel1():
global w
w.destroy()
w = None
def load_song():
mixer.music.load(tk.filedialog.askopenfile())
mixer.music.play()
def pause_song():
mixer.music.pause()
def resume():
mixer.music.unpause()
class Toplevel1:
def __init__(self, top=None):
'''This class configures and populates the toplevel window.
top is the toplevel containing window.'''
_bgcolor = '#d9d9d9' # X11 color: 'gray85'
_fgcolor = '#000000' # X11 color: 'black'
_compcolor = '#d9d9d9' # X11 color: 'gray85'
_ana1color = '#d9d9d9' # X11 color: 'gray85'
_ana2color = '#ececec' # Closest X11 color: 'gray92'
top.geometry("600x430+314+157")
top.minsize(120, 1)
top.maxsize(1370, 729)
top.resizable(1, 1)
top.title("Musik Player Pro ++")
top.configure(relief="groove")
top.configure(background="#000000")
top.configure(highlightbackground="#d9d9d9")
top.configure(highlightcolor="black")
self.menubar = tk.Menu(top,font="TkMenuFont",bg=_bgcolor,fg=_fgcolor)
top.configure(menu = self.menubar)
self.Canvas1 = tk.Canvas(top)
self.Canvas1.place(relx=0.867, rely=0.116, relheight=0.658
, relwidth=0.122)
self.Canvas1.configure(background="#000000")
self.Canvas1.configure(borderwidth="2")
self.Canvas1.configure(highlightbackground="#d9d9d9")
self.Canvas1.configure(highlightcolor="black")
self.Canvas1.configure(insertbackground="#4fff53")
self.Canvas1.configure(relief="ridge")
self.Canvas1.configure(selectbackground="#48ffff")
self.Canvas1.configure(selectforeground="white")
self.Label1 = tk.Label(top)
self.Label1.place(relx=0.667, rely=0.023, height=41, width=194)
self.Label1.configure(activebackground="#f9f9f9")
self.Label1.configure(activeforeground="black")
self.Label1.configure(background="#000000")
self.Label1.configure(disabledforeground="#a3a3a3")
self.Label1.configure(font="-family {Tw Cen MT Condensed} -size 14 -weight bold -slant italic -underline 1")
self.Label1.configure(foreground="#ffffff")
self.Label1.configure(highlightbackground="#d9d9d9")
self.Label1.configure(highlightcolor="black")
self.Label1.configure(text='''Volume''')
self.Scale1 = tk.Scale(top, from_=0.0, to=100.0)
self.Scale1.place(relx=0.767, rely=0.116, relwidth=0.0, relheight=0.665
, width=45, bordermode='ignore')
self.Scale1.configure(activebackground="#ececec")
self.Scale1.configure(background="#000000")
self.Scale1.configure(font="-family {Tw Cen MT Condensed} -size 14 -weight bold -underline 1")
self.Scale1.configure(foreground="#ffffff")
self.Scale1.configure(highlightbackground="#d9d9d9")
self.Scale1.configure(highlightcolor="black")
self.Scale1.configure(label="Volume")
self.Scale1.configure(relief="groove")
self.Scale1.configure(troughcolor="#d9d9d9")
self.tooltip_font = "TkDefaultFont"
self.Scale1_tooltip = \
ToolTip(self.Scale1, self.tooltip_font, '''Volume + / -''')
self.Label1_2 = tk.Label(top)
self.Label1_2.place(relx=0.017, rely=0.023, height=41, width=194)
self.Label1_2.configure(activebackground="#f9f9f9")
self.Label1_2.configure(activeforeground="black")
self.Label1_2.configure(anchor='w')
self.Label1_2.configure(background="#000000")
self.Label1_2.configure(compound='left')
self.Label1_2.configure(disabledforeground="#a3a3a3")
self.Label1_2.configure(font="-family {Tw Cen MT Condensed} -size 14 -weight bold -slant italic -underline 1")
self.Label1_2.configure(foreground="#ffffff")
self.Label1_2.configure(highlightbackground="#d9d9d9")
self.Label1_2.configure(highlightcolor="black")
self.Label1_2.configure(text='''Now playing:''')
self.Label2 = tk.Label(top)
self.Label2.place(relx=0.267, rely=0.279, height=101, width=164)
self.Label2.configure(activebackground="#f9f9f9")
self.Label2.configure(activeforeground="black")
self.Label2.configure(background="#000000")
self.Label2.configure(disabledforeground="#a3a3a3")
self.Label2.configure(font="-family {Segoe UI Black} -size 15 -weight bold -slant italic -underline 1")
self.Label2.configure(foreground="#ffffff")
self.Label2.configure(highlightbackground="#d9d9d9")
self.Label2.configure(highlightcolor="black")
self.Label2.configure(text='''Track Name''')
self.Button1 = tk.Button(top)
self.Button1.place(relx=0.017, rely=0.837, height=64, width=187)
self.Button1.configure(activebackground="#ffffff")
self.Button1.configure(activeforeground="#000000")
self.Button1.configure(background="#000000")
self.Button1.configure(cursor="fleur")
self.Button1.configure(disabledforeground="#a3a3a3")
self.Button1.configure(foreground="#ffffff")
self.Button1.configure(highlightbackground="#d9d9d9")
self.Button1.configure(highlightcolor="black")
self.Button1.configure(pady="0")
self.Button1.configure(relief="groove")
self.Button1.configure(text='''Resume''')
self.Button1.configure(command=resume)
self.Button1_1 = tk.Button(top)
self.Button1_1.place(relx=0.35, rely=0.837, height=64, width=187)
self.Button1_1.configure(activebackground="#ffffff")
self.Button1_1.configure(activeforeground="#000000")
self.Button1_1.configure(background="#000000")
self.Button1_1.configure(disabledforeground="#a3a3a3")
self.Button1_1.configure(foreground="#ffffff")
self.Button1_1.configure(highlightbackground="#d9d9d9")
self.Button1_1.configure(highlightcolor="black")
self.Button1_1.configure(pady="0")
self.Button1_1.configure(relief="groove")
self.Button1_1.configure(text='''Pause''')
self.Button1_1.configure(command=pause_song)
self.Button1_1_1 = tk.Button(top)
self.Button1_1_1.place(relx=0.683, rely=0.837, height=64, width=187)
self.Button1_1_1.configure(activebackground="#ffffff")
self.Button1_1_1.configure(activeforeground="#000000")
self.Button1_1_1.configure(background="#000000")
self.Button1_1_1.configure(disabledforeground="#a3a3a3")
self.Button1_1_1.configure(foreground="#ffffff")
self.Button1_1_1.configure(highlightbackground="#d9d9d9")
self.Button1_1_1.configure(highlightcolor="black")
self.Button1_1_1.configure(pady="0")
self.Button1_1_1.configure(relief="groove")
self.Button1_1_1.configure(text='''Load Song''')
self.Button1_1_1.configure(command=load_song)
from time import time, localtime, strftime
class ToolTip(tk.Toplevel):
"""
Provides a ToolTip widget for Tkinter.
To apply a ToolTip to any Tkinter widget, simply pass the widget to the
ToolTip constructor
"""
def __init__(self, wdgt, tooltip_font, msg=None, msgFunc=None,
delay=0.5, follow=True):
"""
Initialize the ToolTip
Arguments:
wdgt: The widget this ToolTip is assigned to
tooltip_font: Font to be used
msg: A static string message assigned to the ToolTip
msgFunc: A function that retrieves a string to use as the ToolTip text
delay: The delay in seconds before the ToolTip appears(may be float)
follow: If True, the ToolTip follows motion, otherwise hides
"""
self.wdgt = wdgt
# The parent of the ToolTip is the parent of the ToolTips widget
self.parent = self.wdgt.master
# Initalise the Toplevel
tk.Toplevel.__init__(self, self.parent, bg='black', padx=1, pady=1)
# Hide initially
self.withdraw()
# The ToolTip Toplevel should have no frame or title bar
self.overrideredirect(True)
# The msgVar will contain the text displayed by the ToolTip
self.msgVar = tk.StringVar()
if msg is None:
self.msgVar.set('No message provided')
else:
self.msgVar.set(msg)
self.msgFunc = msgFunc
self.delay = delay
self.follow = follow
self.visible = 0
self.lastMotion = 0
# The text of the ToolTip is displayed in a Message widget
tk.Message(self, textvariable=self.msgVar, bg='#FFFFDD',
font=tooltip_font,
aspect=1000).grid()
# Add bindings to the widget. This will NOT override
# bindings that the widget already has
self.wdgt.bind('<Enter>', self.spawn, '+')
self.wdgt.bind('<Leave>', self.hide, '+')
self.wdgt.bind('<Motion>', self.move, '+')
def spawn(self, event=None):
"""
Spawn the ToolTip. This simply makes the ToolTip eligible for display.
Usually this is caused by entering the widget
Arguments:
event: The event that called this funciton
"""
self.visible = 1
# The after function takes a time argument in milliseconds
self.after(int(self.delay * 1000), self.show)
def show(self):
"""
Displays the ToolTip if the time delay has been long enough
"""
if self.visible == 1 and time() - self.lastMotion > self.delay:
self.visible = 2
if self.visible == 2:
self.deiconify()
def move(self, event):
"""
Processes motion within the widget.
Arguments:
event: The event that called this function
"""
self.lastMotion = time()
# If the follow flag is not set, motion within the
# widget will make the ToolTip disappear
#
if self.follow is False:
self.withdraw()
self.visible = 1
# Offset the ToolTip 10x10 pixes southwest of the pointer
self.geometry('+%i+%i' % (event.x_root+20, event.y_root-10))
try:
# Try to call the message function. Will not change
# the message if the message function is None or
# the message function fails
self.msgVar.set(self.msgFunc())
except:
pass
self.after(int(self.delay * 1000), self.show)
def hide(self, event=None):
"""
Hides the ToolTip. Usually this is caused by leaving the widget
Arguments:
event: The event that called this function
"""
self.visible = 0
self.withdraw()
def update(self, msg):
"""
Updates the Tooltip with a new message. Added by Rozen
"""
self.msgVar.set(msg)
# ===========================================================
# End of Class ToolTip
# ===========================================================
if __name__ == '__main__':
vp_start_gui()
HERE IS THE ERROR PICTURE:
https://i.stack.imgur.com/eAs6q.png
https://i.stack.imgur.com/7qwDf.png
Thanks
Don't use a mix of tabs and space for indentation, it's not considered a good practice (python specific).
Now to fix it:
Go to terminal and type pip install autopep8
Once it's installed type the following command :
autopep8 -i yourfilename.py
3)You're good to go now!

Can't change text of label in Tkinter

Hello I've set up a GUI for my Python Pinger but somehow I'm unable to change label placed inside the Main Class of the program.
Here is the main script:
import sys
try:
import Tkinter as tk
except ImportError:
import tkinter as tk
try:
import ttk
py3 = False
except ImportError:
import tkinter.ttk as ttk
py3 = True
import PyngIT_support
def vp_start_gui():
'''Starting point when module is the main routine.'''
global val, w, root
root = tk.Tk()
top = PythonPingIT (root)
PyngIT_support.init(root, top)
root.mainloop()
w = None
def create_PythonPingIT(rt, *args, **kwargs):
'''Starting point when module is imported by another module.
Correct form of call: 'create_PythonPingIT(root, *args, **kwargs)' .'''
global w, w_win, root
#rt = root
root = rt
w = tk.Toplevel (root)
top = PythonPingIT (w)
PyngIT_support.init(w, top, *args, **kwargs)
return (w, top)
def destroy_PythonPingIT():
global w
w.destroy()
w = None
class PythonPingIT:
def __init__(self, top=None):
'''This class configures and populates the toplevel window.
top is the toplevel containing window.'''
_bgcolor = '#d9d9d9' # X11 color: 'gray85'
_fgcolor = '#000000' # X11 color: 'black'
_compcolor = '#d9d9d9' # X11 color: 'gray85'
_ana1color = '#d9d9d9' # X11 color: 'gray85'
_ana2color = '#ececec' # Closest X11 color: 'gray92'
self.style = ttk.Style()
if sys.platform == "win32":
self.style.theme_use('winnative')
self.style.configure('.',background=_bgcolor)
self.style.configure('.',foreground=_fgcolor)
self.style.configure('.',font="TkDefaultFont")
self.style.map('.',background=
[('selected', _compcolor), ('active',_ana2color)])
top.geometry("1200x800+373+87")
top.minsize(176, 1)
top.maxsize(1924, 1050)
top.resizable(1, 1)
top.title("PyngIT")
top.configure(background="#e4e4e4")
top.configure(highlightbackground="#d9d9d9")
top.configure(highlightcolor="black")
self.Frame_StaniceA = tk.LabelFrame(top)
self.Frame_StaniceA.place(relx=0.006, rely=0.0, relheight=0.331
, relwidth=0.985)
self.Frame_StaniceA.configure(relief='groove')
self.Frame_StaniceA.configure(foreground="black")
self.Frame_StaniceA.configure(text='''Stanice A''')
self.Frame_StaniceA.configure(background="#d9d9d9")
self.Frame_StaniceA.configure(highlightbackground="#d9d9d9")
self.Frame_StaniceA.configure(highlightcolor="black")
self.Motol = tk.Label(self.Frame_StaniceA)
self.Motol.place(relx=0.007, rely=0.083, height=31, width=95
, bordermode='ignore')
self.Motol.configure(activebackground="#f9f9f9")
self.Motol.configure(activeforeground="black")
self.Motol.configure(background="#d9d9d9")
self.Motol.configure(disabledforeground="#a3a3a3")
self.Motol.configure(foreground="#000000")
self.Motol.configure(highlightbackground="#d9d9d9")
self.Motol.configure(highlightcolor="black")
self.Motol.configure(text='''N. Motol''')
if __name__ == '__main__':
vp_start_gui()
And here is the support script:
import sys
import multiping
import os
from threading import Thread
try:
import Tkinter as tk
except ImportError:
import tkinter as tk
try:
import ttk
py3 = False
except ImportError:
import tkinter.ttk as ttk
py3 = True
def init(top, gui, *args, **kwargs):
global w, top_level, root
w = gui
top_level = top
root = top
def destroy_window():
# Function which closes the window.
global top_level
top_level.destroy()
top_level = None
def ping_a_comm():
t = Thread(target=ping_A)
t.start()
pass
def ping_A():
import PyngIT as p
os.system("ping 192.168.0.1")
p.PythonPingIT.Motol.configure(text="TEXT")
pass
if __name__ == '__main__':
import PyngIT
PyngIT.vp_start_gui()
When I try to change the label I get "type object 'PythonPingIT' has no attribute 'Motol'".
So I thought that maybe I need to call the class like this:
p.PythonPingIT().Motol.configure(text="text")
but only got this
Exception has occurred: AttributeError
'NoneType' object has no attribute 'geometry'
File "C:\Users\admin\Desktop\PythonPingIt\PyngIT.py", line 67, in __init__
top.geometry("1200x800+373+87")
File "C:\Users\admin\Desktop\PythonPingIt\PyngIT_support.py", line 45, in ping_A
p.PythonPingIT().Motol.configure(text="hey")
I am really lost and if anyone knows how to solve this, I would be really grateful
EDIT:// I managed to access the value with this code:
def ping_A():
global w, top_level, root
import PyngIT as p
root = tk.Tk()
w = p.PythonPingIT (root)
os.system("ping localhost")
w.Motol.configure(text="FUUUu")
pass
But I still am unable to change the text, even with w.Motol.pack() or w.update()

Live Stream Camera in Label Using Tkinter python2

i am tying to live stream camera in label using tkinter with opencv and python2 but problem is when i click on button only one frame display in label.
#! /usr/bin/env python
# -- coding: utf-8 --
#
# GUI module generated by PAGE version 4.22
# in conjunction with Tcl version 8.6
# May 17, 2019 06:44:40 PM PKT platform: Linux
.
import sys
try:
import Tkinter as tk
except ImportError:
import tkinter as tk
try:
import ttk
py3 = False
except ImportError:
import tkinter.ttk as ttk
py3 = True
import gui_support
from Tkinter import *
from PIL import ImageTk, Image
import cv2
def vp_start_gui():
'''Starting point when module is the main routine.'''
global val, w, root
root = tk.Tk()
top = Toplevel1 (root)
gui_support.init(root, top)
root.mainloop()
w = None
def create_Toplevel1(root, *args, **kwargs):
'''Starting point when module is imported by another program.'''
global w, w_win, rt
rt = root
w = tk.Toplevel (root)
top = Toplevel1 (w)
gui_support.init(w, top, *args, **kwargs)
return (w, top)
def destroy_Toplevel1():
global w
w.destroy()
w = None
class Toplevel1:
def __init__(self, top=None):
'''This class configures and populates the toplevel window.
top is the toplevel containing window.'''
_bgcolor = '#d9d9d9' # X11 color: 'gray85'
_fgcolor = '#000000' # X11 color: 'black'
_compcolor = '#d9d9d9' # X11 color: 'gray85'
_ana1color = '#d9d9d9' # X11 color: 'gray85'
_ana2color = '#ececec' # Closest X11 color: 'gray92'
self.style = ttk.Style()
if sys.platform == "win32":
self.style.theme_use('winnative')
self.style.configure('.',background=_bgcolor)
self.style.configure('.',foreground=_fgcolor)
self.style.configure('.',font="TkDefaultFont")
self.style.map('.',background=
[('selected', _compcolor), ('active',_ana2color)])
top.geometry("1308x647+69+51")
top.title("New Toplevel")
self.menubar = tk.Menu(top,font="TkMenuFont",bg=_bgcolor,fg=_fgcolor)
top.configure(menu = self.menubar)
self.Label1 = tk.Label(top)
self.Label1.place(relx=0.268, rely=0.015, height=31, width=599)
self.Label1.configure(font="-family {Bitstream Vera Sans} -size 21 -weight bold")
self.Label1.configure(text='''Face ID Attendance Marking System''')
self.Label1.configure(width=599)
self.TSeparator1 = ttk.Separator(top)
self.TSeparator1.place(relx=0.245, rely=0.077, relwidth=0.497)
self.TPanedwindow1 = ttk.Panedwindow(top, orient="horizontal")
self.TPanedwindow1.place(relx=0.008, rely=0.139, relheight=0.68
, relwidth=0.982)
self.TPanedwindow1.configure(width=1285)
self.TPanedwindow1_p1 = ttk.Labelframe(width=650, text='Pane 1')
self.TPanedwindow1.add(self.TPanedwindow1_p1)
self.TPanedwindow1_p2 = ttk.Labelframe(text='Pane 2')
self.TPanedwindow1.add(self.TPanedwindow1_p2)
self.__funcid0 = self.TPanedwindow1.bind('<Map>', self.__adjust_sash0)
self.Label2 = tk.Label(self.TPanedwindow1_p1)
self.Label2.place(relx=0.016, rely=0.045, height=411, width=619
, bordermode='ignore')
self.Label2.configure(text='''Label''')
self.Label2.configure(width=619)
self.TButton1 = ttk.Button(top)
self.TButton1.place(relx=0.031, rely=0.835, height=28, width=83)
self.TButton1.configure(takefocus="")
self.TButton1.configure(text='''Tbutton''')
self.TButton1.configure(command = self.cameratk)
def __adjust_sash0(self, event):
paned = event.widget
pos = [640, ]
i = 0
for sash in pos:
paned.sashpos(i, sash)
i += 1
paned.unbind('<map>', self.__funcid0)
del self.__funcid0
def cameratk(self):
cap = cv2.VideoCapture(0)
_, frame = cap.read()
cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
img = Image.fromarray(cv2image)
imgtk = ImageTk.PhotoImage(image=img)
self.Label2.imgtk = imgtk
self.Label2.configure(image=imgtk)
self.Label2.after(1, video_stream)
if __name__ == '__main__':
vp_start_gui()
Solved
self.Label2.after(1, video_stream)
Video_Stream Replaced by Function name as self.cameratk
self.Label2.after(1, self.cameratk)
and cap = cv2.VideoCapture(0) outside from the function.

Getting weird indentation error on python tkinter - unindent does not match any outer indentation level

I'm just adding 1 def with 2 lines of code before it gets indentation error. I'm trying to modify the code that generated from PAGE Tkinter GUI editor for login message box. I really don't know why this happens.
I did the exact same code as this guy on youtube and I tried to move the def into the far-most left neither right.
So what I'm trying to add is. def cancelLogin() inside the login page class. It shows me indentation error on def init(). Please help me to figure this out.
#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# GUI module generated by PAGE version 4.20
# in conjunction with Tcl version 8.6
# Feb 07, 2019 05:38:55 AM EST platform: Linux
import sys
import tkMessageBox
try:
import Tkinter as tk
except ImportError:
import tkinter as tk
try:
import ttk
py3 = False
except ImportError:
import tkinter.ttk as ttk
py3 = True
import jta_support
def vp_start_gui():
'''Starting point when module is the main routine.'''
global val, w, root
root = tk.Tk()
top = Login_page (root)
jta_support.init(root, top)
root.mainloop()
w = None
def create_Login_page(root, *args, **kwargs):
'''Starting point when module is imported by another program.'''
global w, w_win, rt
rt = root
w = tk.Toplevel (root)
top = Login_page (w)
jta_support.init(w, top, *args, **kwargs)
return (w, top)
def destroy_Login_page():
global w
w.destroy()
w = None
class Login_page:
def cancelLogin(self):
msg=tkMessageBox.askyesno("Login page","Are you sure , you want to cancel login?")
def __init__(self, top=None):
'''This class configures and populates the toplevel window.
top is the toplevel containing window.'''
_bgcolor = '#d9d9d9' # X11 color: 'gray85'
_fgcolor = '#000000' # X11 color: 'black'
_compcolor = '#d9d9d9' # X11 color: 'gray85'
_ana1color = '#d9d9d9' # X11 color: 'gray85'
_ana2color = '#ececec' # Closest X11 color: 'gray92'
font11 = "-family {DejaVu Sans} -size 10 -weight bold -slant " \
"roman -underline 0 -overstrike 0"
font17 = "-family {DejaVu Sans} -size 13 -weight bold -slant " \
"roman -underline 0 -overstrike 0"
self.style = ttk.Style()
if sys.platform == "win32":
self.style.theme_use('winnative')
self.style.configure('.',background=_bgcolor)
self.style.configure('.',foreground=_fgcolor)
self.style.configure('.',font="TkDefaultFont")
self.style.map('.',background=
[('selected', _compcolor), ('active',_ana2color)])
top.geometry("584x457+1039+55")
top.title("JTA Login")
top.configure(relief="ridge")
top.configure(background="#201856")
top.configure(highlightbackground="#000000")
self.btnLogin = tk.Button(top)
self.btnLogin.place(relx=0.634, rely=0.744, height=54, width=101)
self.btnLogin.configure(background="#201856")
self.btnLogin.configure(borderwidth="0")
self.btnLogin.configure(font=font11)
self.btnLogin.configure(highlightbackground="#201856")
self._img1 = tk.PhotoImage(file="../../Downloads/button.png")
self.btnLogin.configure(image=self._img1)
self.btnLogin.configure(padx="6m")
self.btnLogin.configure(pady="3m")
self.btnLogin.configure(text='''Login''')
self.btnLogin.configure(width=101)
self.btnExit = tk.Button(top)
self.btnExit.place(relx=0.223, rely=0.744, height=55, width=97)
self.btnExit.configure(activebackground="#ffffff")
self.btnExit.configure(background="#201856")
self.btnExit.configure(borderwidth="0")
self.btnExit.configure(font=font11)
self.btnExit.configure(highlightbackground="#201856")
self._img2 = tk.PhotoImage(file="../../Downloads/button(1).png")
self.btnExit.configure(image=self._img2)
self.btnExit.configure(padx="6m")
self.btnExit.configure(pady="3m")
self.btnExit.configure(text='''Exit''')
self.btnExit.configure(width=97)
self.txtUsername = tk.Entry(top)
self.txtUsername.place(relx=0.462, rely=0.372, height=40, relwidth=0.342)
self.txtUsername.configure(background="white")
self.txtUsername.configure(font="TkFixedFont")
self.txtPassword = tk.Entry(top)
self.txtPassword.place(relx=0.462, rely=0.547, height=40, relwidth=0.342)
self.txtPassword.configure(background="white")
self.txtPassword.configure(font="TkFixedFont")
self.txtPassword.configure(selectbackground="#c4c4c4")
self.Label1 = tk.Label(top)
self.Label1.place(relx=0.205, rely=0.372, height=40, width=100)
self.Label1.configure(background="#201856")
self.Label1.configure(font=font17)
self.Label1.configure(foreground="#ffffff")
self.Label1.configure(text='''Username''')
self.Label1_3 = tk.Label(top)
self.Label1_3.place(relx=0.205, rely=0.547, height=40, width=100)
self.Label1_3.configure(activebackground="#f9f9f9")
self.Label1_3.configure(background="#201856")
self.Label1_3.configure(font=font17)
self.Label1_3.configure(foreground="#ffffff")
self.Label1_3.configure(text='''Password''')
self.menubar = tk.Menu(top,font="TkMenuFont",bg=_bgcolor,fg=_fgcolor)
top.configure(menu = self.menubar)
self.TLabel1 = ttk.Label(top)
self.TLabel1.place(relx=0.154, rely=0.022, height=131, width=423)
self.TLabel1.configure(background="#201856")
self.TLabel1.configure(foreground="#000000")
self.TLabel1.configure(font="TkDefaultFont")
self.TLabel1.configure(relief='flat')
self.TLabel1.configure(text='''Tlabel''')
self.TLabel1.configure(width=423)
self._img3 = tk.PhotoImage(file="./jtalogo.png")
self.TLabel1.configure(image=self._img3)
if __name__ == '__main__':
vp_start_gui()
without the "def cancelLogin()" it works fine as usual
actual result :
File "./jta.py", line 52
def init(self, top=None):
^
IndentationError: unindent does not match any outer indentation level
You can try:
grep -C 7 "__init" YOUR_FILE.py | tr ' \t' '.*'
This will translate spaces into dots, and tabs into stars. Then you can check if there is a mismatch there.
This is the output when I tried it on the code you posted (from the question source):
$ grep -C 7 "__init" test.py | tr ' \t' '.*'
........w.=.None
....
....class.Login_page:
....
....*def.cancelLogin(self):
....**msg=tkMessageBox.askyesno("Login.page","Are.you.sure.,.you.want.to.cancel.login?")
....
........def.__init__(self,.top=None):
............'''This.class.configures.and.populates.the.toplevel.window.
...............top.is.the.toplevel.containing.window.'''
............_bgcolor.=.'#d9d9d9'..#.X11.color:.'gray85'
............_fgcolor.=.'#000000'..#.X11.color:.'black'
............_compcolor.=.'#d9d9d9'.#.X11.color:.'gray85'
............_ana1color.=.'#d9d9d9'.#.X11.color:.'gray85'.
............_ana2color.=.'#ececec'.#.Closest.X11.color:.'gray92'.
As stated by #tobias_k, there are tabs and spaces mixed in the lines you added.

AttributeError: 'module' object has no attribute 'key'

I am using tkinter and i have 2 .py files, one called "account" and another called "account_support" im trying to build the gui in the "account.py" and then have all the functions related to that gui in "account_support.py"
this is account.py
import sys
try:
from Tkinter import *
except ImportError:
from tkinter import *
try:
import ttk
py3 = 0
except ImportError:
import tkinter.ttk as ttk
py3 = 1
import account_support
def vp_start_gui():
'''Starting point when module is the main routine.'''
global val, w, root
root = Tk()
top = Add_Account (root)
account_support.init(root, top)
root.mainloop()
w = None
def create_Add_Account(root, *args, **kwargs):
'''Starting point when module is imported by another program.'''
global w, w_win, rt
rt = root
w = Toplevel (root)
top = Add_Account (w)
account_support.init(w, top, *args, **kwargs)
return (w, top)
def destroy_Add_Account():
global w
w.destroy()
w = None
class Add_Account:
def __init__(self, top=None):
'''This class configures and populates the toplevel window.
top is the toplevel containing window.'''
_bgcolor = '#d9d9d9' # X11 color: 'gray85'
_fgcolor = '#000000' # X11 color: 'black'
_compcolor = '#d9d9d9' # X11 color: 'gray85'
_ana1color = '#d9d9d9' # X11 color: 'gray85'
_ana2color = '#d9d9d9' # X11 color: 'gray85'
font9 = "-family {DejaVu Sans} -size 0 -weight normal -slant " \
"roman -underline 0 -overstrike 0"
top.geometry("400x200+407+266")
top.title("Add Account")
top.configure(highlightcolor="black")
self.menubar = Menu(top,font=font9,bg=_bgcolor,fg=_fgcolor)
top.configure(menu = self.menubar)
self.accName = Entry(top)
self.accName.place(relx=0.35, rely=0.3, relheight=0.11, relwidth=0.44)
self.accName.configure(background="white")
self.accName.configure(font="TkFixedFont")
self.accName.configure(selectbackground="#c4c4c4")
self.Label1 = Label(top)
self.Label1.place(relx=0.25, rely=0.1, height=38, width=176)
self.Label1.configure(activebackground="#f9f9f9")
self.Label1.configure(text='''Add Account''')
self.Label2 = Label(top)
self.Label2.place(relx=0.03, rely=0.3, height=18, width=126)
self.Label2.configure(activebackground="#f9f9f9")
self.Label2.configure(text='''Account Name:''')
self.key = Entry(top)
self.key.place(relx=0.35, rely=0.5, relheight=0.11, relwidth=0.44)
self.key.configure(background="white")
self.key.configure(font="TkFixedFont")
self.key.configure(selectbackground="#c4c4c4")
self.Label3 = Label(top)
self.Label3.place(relx=0.15, rely=0.5, height=21, width=76)
self.Label3.configure(activebackground="#f9f9f9")
self.Label3.configure(text='''CMA Key:''')
self.add = Button(top)
self.add.place(relx=0.53, rely=0.7, height=26, width=107)
self.add.configure(activebackground="#d9d9d9")
self.add.configure(command=account_support.addaccount)
self.add.configure(text='''Add Account''')
self.helpButton = Button(top)
self.helpButton.place(relx=0.8, rely=0.5, height=16, width=17)
self.helpButton.configure(activebackground="#d9d9d9")
self.helpButton.configure(command=account_support.question)
self.helpButton.configure(text='''?''')
if __name__ == '__main__':
vp_start_gui()
and then for account_support.py
import sys
import webbrowser
import tkMessageBox
try:
from Tkinter import *
except ImportError:
from tkinter import *
try:
import ttk
py3 = 0
except ImportError:
import tkinter.ttk as ttk
py3 = 1
def addaccount():
import account
cmakey = account.Entry.key.get()
acc = account.Entry.accName.get()
print acc
print cmakey
sys.stdout.flush()
def question():
tkMessageBox.showinfo(title="CMA Key", message="")
sys.stdout.flush()
def init(top, gui, *args, **kwargs):
global w, top_level, root
w = gui
top_level = top
root = top
def destroy_window():
# Function which closes the window.
global top_level
top_level.destroy()
top_level = None
if __name__ == '__main__':
import account
account.vp_start_gui()
when i try to get the value of account.py's key entry
with
def addaccount():
import account
cmakey = account.Entry.key.get()
acc = account.Entry.accName.get()
print acc
print cmakey
sys.stdout.flush()
i get the error
File "/home/silicaandpina/VitaTools/psvimgtools-0.1-linux64/gui/psvimgtools/account_support.py", line 29, in addaccount
cmakey = account.Entry.key.get()
AttributeError: class Entry has no attribute 'key'
When you're executing
cmakey = account.Entry.key.get()
acc = account.Entry.accName.get()
you're trying to access the key property of the Entry class of Tkinter in your module account. As you can tell by the error message, the Entry class doesn't have a property called key.
The Solution
Change your addaccount function to
def addaccount(cmakey, acc):
print acc
print cmakey
sys.stdout.flush()
As you can see, you're now receiving two parameters that are already the strings you want to use. Now, in account, change the button configuration line to
self.add.configure(command=lambda: account_support.addaccount(self.key.get(), self.accName.get()))

Categories