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.
Related
main.py
import sys
import tkinter as tk
import tkinter.ttk as ttk
from tkinter.constants import *
from PIL import ImageTk, Image
from tkinter import *
import main_support
def popup1(event, *args, **kwargs):
Popupmenu1 = tk.Menu(main_support.root, tearoff=0)
Popupmenu1.configure(activebackground="#ececec")
Popupmenu1.configure(activeborderwidth="1")
Popupmenu1.configure(activeforeground="#000000")
Popupmenu1.configure(background="#d9d9d9")
Popupmenu1.configure(borderwidth="1")
Popupmenu1.configure(disabledforeground="#a3a3a3")
Popupmenu1.configure(font="-family {Segoe UI} -size 9")
Popupmenu1.configure(foreground="#000000")
Popupmenu1.post(event.x_root, event.y_root)
class Header:
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()
self.methodSelectionValue = []
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("600x450+664+264")
top.minsize(120, 1)
top.maxsize(1924, 1061)
top.resizable(1, 1)
top.title("Toplevel 0")
top.configure(background="#d9d9d9")
top.configure(highlightbackground="#f0f0f0f0f0f0")
top.configure(highlightcolor="black")
self.top = top
self.combobox = tk.StringVar()
self.Label1 = tk.Label(self.top)
self.Label1.place(relx=-0.033, rely=0.0, height=41, width=634)
self.Label1.configure(activebackground="#f9f9f9")
self.Label1.configure(activeforeground="black")
self.Label1.configure(background="#b90746")
self.Label1.configure(compound='center')
self.Label1.configure(disabledforeground="#a3a3a3")
self.Label1.configure(font="-family {Calisto MT} -size 16 -weight bold -slant italic")
self.Label1.configure(foreground="#ffffff")
self.Label1.configure(highlightbackground="#d9d9d9")
self.Label1.configure(highlightcolor="black")
self.Label1.configure(text='''Calculator''')
self.TFrame1 = ttk.Frame(self.top)
self.TFrame1.place(relx=0.02, rely=0.12, relheight=0.856, relwidth=0.958)
self.TFrame1.configure(relief='groove')
self.TFrame1.configure(borderwidth="2")
self.TFrame1.configure(relief="groove")
self.Labelframe1 = tk.LabelFrame(self.TFrame1)
self.Labelframe1.place(relx=0.47, rely=0.026, relheight=0.948, relwidth=0.504)
self.Labelframe1.configure(relief='groove')
self.Labelframe1.configure(foreground="black")
self.Labelframe1.configure(text='''Main Window''')
self.Labelframe1.configure(background="#d9d9d9")
self.Labelframe1.configure(highlightbackground="#d9d9d9")
self.Labelframe1.configure(highlightcolor="black")
self.Label2 = tk.Label(self.Labelframe1)
self.Label2.place(relx=0.038, rely=0.11, height=31, width=90, bordermode='ignore')
self.Label2.configure(activebackground="#f9f9f9")
self.Label2.configure(activeforeground="black")
self.Label2.configure(anchor='w')
self.Label2.configure(background="#d9d9d9")
self.Label2.configure(compound='left')
self.Label2.configure(disabledforeground="#a3a3a3")
self.Label2.configure(foreground="#000000")
self.Label2.configure(highlightbackground="#d9d9d9")
self.Label2.configure(highlightcolor="black")
self.Label2.configure(text='''Class Selection''')
self.TCombobox1 = ttk.Combobox(self.Labelframe1, values=['Basic', 'Advance'])
self.TCombobox1.bind("<<ComboboxSelected>>", self.setMethodValue)
self.TCombobox1.place(relx=0.421, rely=0.121, relheight=0.058, relwidth=0.531, bordermode='ignore')
self.TCombobox1.configure(textvariable=self.combobox)
self.TCombobox1.configure(takefocus="")
self.Label3 = tk.Label(self.Labelframe1)
self.Label3.place(relx=0.038, rely=0.274, height=21, width=111, bordermode='ignore')
self.Label3.configure(activebackground="#f9f9f9")
self.Label3.configure(activeforeground="black")
self.Label3.configure(anchor='w')
self.Label3.configure(background="#d9d9d9")
self.Label3.configure(compound='left')
self.Label3.configure(disabledforeground="#a3a3a3")
self.Label3.configure(foreground="#000000")
self.Label3.configure(highlightbackground="#d9d9d9")
self.Label3.configure(highlightcolor="black")
self.Label3.configure(text='''Method Selection''')
self.TCombobox2 = ttk.Combobox(self.Labelframe1, values=self.methodSelectionValue)
self.TCombobox2.place(relx=0.414, rely=0.274, relheight=0.058, relwidth=0.528, bordermode='ignore')
self.TCombobox2.configure(textvariable=self.combobox)
self.TCombobox2.configure(takefocus="")
self.TEntry1 = ttk.Entry(self.Labelframe1)
self.TEntry1.place(relx=0.138, rely=0.493, relheight=0.112, relwidth=0.262, bordermode='ignore')
self.TEntry1.configure(takefocus="")
self.TEntry1.configure(cursor="ibeam")
self.Label4 = tk.Label(self.Labelframe1)
self.Label4.place(relx=0.207, rely=0.411, height=21, width=74, bordermode='ignore')
self.Label4.configure(activebackground="#f9f9f9")
self.Label4.configure(activeforeground="black")
self.Label4.configure(anchor='w')
self.Label4.configure(background="#d9d9d9")
self.Label4.configure(compound='left')
self.Label4.configure(disabledforeground="#a3a3a3")
self.Label4.configure(foreground="#000000")
self.Label4.configure(highlightbackground="#d9d9d9")
self.Label4.configure(highlightcolor="black")
self.Label4.configure(text='''Input 1''')
self.TEntry2 = ttk.Entry(self.Labelframe1)
self.TEntry2.place(relx=0.621, rely=0.493, relheight=0.112, relwidth=0.262, bordermode='ignore')
self.TEntry2.configure(takefocus="")
self.TEntry2.configure(cursor="ibeam")
self.Label5 = tk.Label(self.Labelframe1)
self.Label5.place(relx=0.655, rely=0.411, height=21, width=44, bordermode='ignore')
self.Label5.configure(activebackground="#f9f9f9")
self.Label5.configure(activeforeground="black")
self.Label5.configure(anchor='w')
self.Label5.configure(background="#d9d9d9")
self.Label5.configure(compound='left')
self.Label5.configure(disabledforeground="#a3a3a3")
self.Label5.configure(foreground="#000000")
self.Label5.configure(highlightbackground="#d9d9d9")
self.Label5.configure(highlightcolor="black")
self.Label5.configure(text='''Input 2''')
self.Button1 = tk.Button(self.Labelframe1)
self.Button1.place(relx=0.397, rely=0.685, height=34, width=57, bordermode='ignore')
self.Button1.configure(activebackground="#ececec")
self.Button1.configure(activeforeground="#000000")
self.Button1.configure(background="#d9d9d9")
self.Button1.configure(compound='left')
self.Button1.configure(disabledforeground="#a3a3a3")
self.Button1.configure(foreground="#000000")
self.Button1.configure(highlightbackground="#d9d9d9")
self.Button1.configure(highlightcolor="black")
self.Button1.configure(pady="0")
self.Button1.configure(text='''Run''')
self.TFrame2 = ttk.Frame(self.TFrame1)
self.TFrame2.place(relx=0.017, rely=0.026, relheight=0.948, relwidth=0.426)
self.TFrame2.configure(relief='groove')
self.TFrame2.configure(borderwidth="2")
self.TFrame2.configure(relief="groove")
def setMethodValue(self, event):
selectedValue = self.TCombobox1.get()
if selectedValue == "Basic":
self.TCombobox2["values"] = ["Add", "Sub"]
elif selectedValue == "Advance":
self.TCombobox2["values"] = ["Multiply", "Division", "Modulus"]
self.TCombobox2.current()
def start_up():
main_support.mains()
if __name__ == '__main__':
main_support.mains()
main_support.py
import sys
import tkinter as tk
import tkinter.ttk as ttk
from tkinter.constants import *
import main
def mains(*args):
'''Main entry point for the application.'''
global root
root = tk.Tk()
root.protocol( 'WM_DELETE_WINDOW' , root.destroy)
# Creates a toplevel widget.
global _top1, _w1
_top1 = root
_w1 = main.Header(_top1)
root.mainloop()
if __name__ == '__main__':
main.start_up()
Created two dropdowns one is for class selection and other is for method selection of that particular class. class name: basic, advance, method selection: basic: add,sub, advance:multiply, division and modulus. currently struck at issue where i select class as basic, it shows basic in method selection dropdown also which is not inteded. thereafter if i select class as basic and in method i select add, class which has to be basic is also getting changed to add. help me out.
You have used same tkinter variable self.combobox for both Combobox widgets.
Remove the line:
self.TCombobox2.configure(textvariable=self.combobox)
Or use another tkinter variable for self.TCombobox2.
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()
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.
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.
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()))