Python Forum

Full Version: tkinter project
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
First Post!
I am new to python but have some experience in VB and a little C!
Building a machine monitoring project that collects data from a machine ie how many made and status etc, this will potentially have 64 inputs to read. It is currently under development so lots of loose ends but functional and i am running this under Python 3.4
I have (what i call) 4 modules (scripts?)

sub.py- this is a routine that just emulates a count
import time
import GUI
import Global_Names as gn

def num():
    for x in range(10):
        time.sleep(0.1)
        print("B", x)
        gn.batchA=x
        #GUI.gui.countAside()
        
Global_Names.py- this just holds global variables to be passed around

global batchA
global batchB
global totalA
global totalB
batchA=10 #batch A
batchB=0 #batch B
totalA=0 #total A
totalB=0 #total B
Main.py- this is where i run the main code from, i have created 2 threads, one to run the tkinter (GUI.py) and one to run the count (num.py)

from GUI import gui
from sub import num
import Global_Names as gn


from threading import Thread


thread=Thread(target=num)
thread.start()

thread=Thread(target=gui)
thread.start()

print(gn.batchA)
GUI.py- this is the main interface

from tkinter import *
from tkinter import ttk
import tkinter as tk
import tkinter.font as font
from tkinter import messagebox
import time
import _thread
import threading
from configparser import ConfigParser
import os
import Global_Names as gn
from multiprocessing import Process


def gui():
 
       

    machinename="Buffoli 1"
    istwosides=True

    myPad=7 #allows quick adjustment of padding

    maint=False


    def myExit():
        
        root.destroy()
        sys.exit()

    def resetBatches():
        gn.batchA=0
        gn.batchB=0
        txtbatchA.set(str(gn.batchA))
        txtbatchB.set(str(gn.batchB))
        txttotalbatch.set(str(gn.batchA+gn.batchB))


    def resetTotals():
        gn.totalA=0
        gn.totalB=0
        txttotalA.set(str(gn.totalA))
        txttotalB.set(str(gn.totalB))
        txttotaltotal.set(str(gn.totalA+gn.totalB))

    def countAside():
        #gn.batchA+=1
        #gn.totalA+=1
        print("W")
        print(gn.batchA)
        txtbatchA.set(str(gn.batchA))
        txttotalA.set(str(gn.totalA))
        txttotalbatch.set(str(gn.batchA+gn.batchB))
        txttotaltotal.set(str(gn.totalA+gn.totalB))

    def countBside():
        gn.batchB+=10
        gn.totalB+=10
        txtbatchB.set(str(gn.batchA))
        txttotalB.set(str(gn.totalA))
        txttotalbatch.set(str(gn.batchA+gn.batchB))
        txttotaltotal.set(str(gn.totalA+gn.totalB))
        tk.messagebox.showinfo("Title", "a Tk MessageBox")

    def scanBarcode(): # Future development for barcode entry #
            pass

    def manualBarcode(): # Job number manual entry #
            top = Toplevel()
            top.title("Manual Barcode Entry")
            top(master=none, height=400, width=400)
            top.overrideredirect(1)
            msg = Message(top, text='about_message')
            msg.pack()

            button = Button(top, text="Dismiss", command=top.destroy)
            button.pack()
            

    def myMaint():

        pass



    ## checks to see if the main form has been exited, if true then raises and error and quits the thread
    def monitorGPIO():
         pass
    

    def receivecountA():
            print("Count A")

    def receivecountB():
            Print("Count B")
            

       
    root = tk.Tk()
    root.title("My Form")
    root.resizable(width=False, height=False)
    root.geometry('800x480')
    root.overrideredirect(1)


    var = tk.StringVar() #used to get the 'value' property of a tkinter.Radiobutton



    txtbatchA=StringVar()
    txtbatchB=StringVar()
    txttotalA=StringVar()
    txttotalB=StringVar()
    txttotalbatch=StringVar()
    txttotaltotal=StringVar()

    txtbatchA.set(str(gn.batchA))
    txtbatchB.set(str(gn.batchB))
    txttotalA.set(str(gn.totalA))
    txttotalB.set(str(gn.totalB))
    #txttotalbatch.set(str(gn.batchA+gn.batchB))
    #txttotaltotal.set(str(gn.totalA+gn.totalB))



    mainframe = ttk.Frame(root, padding="10 0 0 0") #this is the area around the internal window like margin , padding="10 10 3 3"
    mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
    mainframe.columnconfigure(0, weight=1)
    mainframe.rowconfigure(0, weight=1)



    ## Row 0 ##

    b=ttk.Label(mainframe, text="Flowflex Machine Interface",font = ('Sans','20','bold','underline')).grid(column=0, row=0, columnspan=7, pady=10, sticky=(N))


    ## Row 1 ##

    ## mainframe=main window, text=text on button, variable=shared varible for group, indicatron=?, activebackground=inital click colour, selectcolor=button down colour 
    b = Radiobutton(mainframe, wraplength=100, text='Running', variable=var, value="Running", indicatoron=0, activebackground='yellow', selectcolor='green2', width=10, height=2,  font = ('Sans','12','bold'))
    b.grid(row=1, column=1, padx=myPad, pady=myPad)
    b = Radiobutton(mainframe, wraplength=100, text='Setting', variable=var, value="Setting", indicatoron=0, activebackground='yellow', selectcolor='red', width=10, height=2,  font = ('Sans','12','bold'))
    b.grid(row=1, column=2, padx=myPad, pady=myPad)
    b = Radiobutton(mainframe, wraplength=100, text='Maint.', variable=var, value="Maint.", indicatoron=0, activebackground='yellow', selectcolor='red2', width=10, height=2,  font = ('Sans','12','bold'))
    b.grid(row=1, column=3, padx=myPad, pady=myPad)
    b = Radiobutton(mainframe, wraplength=100, text='Breakdown', variable=var, value="Breakdown", indicatoron=0, activebackground='yellow', selectcolor='red2', width=10, height=2,  font = ('Sans','12','bold'))
    b.grid(row=1, column=4, padx=myPad, pady=myPad)
    b = Radiobutton(mainframe, wraplength=100, text='Waiting for Work', variable=var, value="Waiting for Work", indicatoron=0, activebackground='yellow', selectcolor='red2', width=10, height=2,  font = ('Sans','12','bold'))
    b.grid(row=1, column=5, padx=myPad, pady=myPad)
    b = Radiobutton(mainframe, wraplength=100, text='Ready to Run', variable=var, value="Ready to Run", indicatoron=0, activebackground='yellow', selectcolor='red2', width=10, height=2,  font = ('Sans','12','bold'))
    b.grid(row=1, column=6, padx=myPad, pady=myPad)

    ## Row 2 ##

    b = Radiobutton(mainframe, wraplength=100, text='Warm-up', variable=var, value="Warm-up", indicatoron=0, activebackground='yellow', selectcolor='red2', width=10, height=2,  font = ('Sans','12','bold'))
    b.grid(row=2, column=1, padx=myPad, pady=myPad)
    b.invoke()
    b = Radiobutton(mainframe, wraplength=100, text='Future', variable=1, value="3", indicatoron=0, activebackground='yellow', selectcolor='red', width=10, height=2,  font = ('Sans','12','bold'), state=DISABLED)
    b.grid(row=2, column=2, padx=myPad, pady=myPad)
    b = Radiobutton(mainframe, wraplength=100, text='Future', variable=1, value="5", indicatoron=0, activebackground='yellow', selectcolor='red2', width=10, height=2,  font = ('Sans','12','bold'), state=DISABLED)
    b.grid(row=2, column=3, padx=myPad, pady=myPad)
    b = Radiobutton(mainframe, wraplength=100, text='Future', variable=1, value="7", indicatoron=0, activebackground='yellow', selectcolor='red2', width=10, height=2,  font = ('Sans','12','bold'), state=DISABLED)
    b.grid(row=2, column=4, padx=myPad, pady=myPad)
    b = Radiobutton(mainframe, wraplength=100, text='Future', variable=1, value="9", indicatoron=0, activebackground='yellow', selectcolor='red2', width=10, height=2,  font = ('Sans','12','bold'), state=DISABLED)
    b.grid(row=2, column=5, padx=myPad, pady=myPad)
    b = Radiobutton(mainframe, wraplength=100, text='Future', variable=1, value="11", indicatoron=0, activebackground='yellow', selectcolor='red2', width=10, height=2,  font = ('Sans','12','bold'), state=DISABLED)
    b.grid(row=2, column=6, padx=myPad, pady=myPad)


    ## Exit Button ##
    b = Button( wraplength=100, text='EXIT', command=myExit, activebackground='yellow', width=5, height=1,  font = ('Sans','12','bold'), padx=5, pady=5)
    b.place(x=680, y=420)

    ## Machine Label##
    b = Label( wraplength=100, text=machinename, width=15, height=1,  font = ('Sans','12','bold'), padx=10, pady=0, justify=LEFT, anchor="w")
    b.place(x=10, y=450)

    ## Counter Info ##
    b = Label(mainframe, wraplength=100, text="A Side", width=10, height=1,  font = ('Sans','12','bold'))
    b.grid(row=3, column=3, pady=(20,0))
    b = Label(mainframe, wraplength=100, text="B Side", width=10, height=1,  font = ('Sans','12','bold'))
    b.grid(row=3, column=4, pady=(20,0))
    b = Label(mainframe, wraplength=100, text="Totals", width=10, height=1,  font = ('Sans','12','bold'))
    b.grid(row=3, column=5, pady=(20,0))


    b = Label(mainframe, wraplength=100, text="Batch Count", width=10, height=1,  font = ('Sans','10','bold'))
    b.grid(row=4, column=2, pady=(20,0))
    b = Label(mainframe, wraplength=100, text="Total Count", width=10, height=1,  font = ('Sans','10','bold'))
    b.grid(row=5, column=2, pady=(20,0))

    ## A Batch ##
    b = Label(mainframe, wraplength=100, textvariable=txtbatchA, width=10, height=1,  font = ('Sans','12','bold'))
    b.grid(row=4, column=3, pady=(20,0))
    ## B Batch ##
    b = Label(mainframe, wraplength=100, textvariable=txtbatchB, width=10, height=1,  font = ('Sans','12','bold'))
    b.grid(row=4, column=4, pady=(20,0))
    ## A Total ##
    b = Label(mainframe, wraplength=100, textvariable=txttotalA, width=10, height=1,  font = ('Sans','12','bold'))
    b.grid(row=5, column=3, pady=(20,0))
    ## B Total ##
    b = Label(mainframe, wraplength=100, textvariable=txttotalB, width=10, height=1,  font = ('Sans','12','bold'))
    b.grid(row=5, column=4, pady=(20,0))
    ## Batch Total ##
    b = Label(mainframe, wraplength=100, textvariable=txttotalbatch, width=10, height=1,  font = ('Sans','12','bold'))
    b.grid(row=4, column=5, pady=(20,0))
    ## Totals Total ##
    b = Label(mainframe, wraplength=100, textvariable=txttotaltotal, width=10, height=1,  font = ('Sans','12','bold'))
    b.grid(row=5, column=5, pady=(20,0))

    ## Reset Buttons ##
    b = Button(mainframe, wraplength=70, text='Reset Batches',command=resetBatches, activebackground='yellow', width=10, height=1,  font = ('Sans','10'), padx=10, pady=10)
    b.grid(row=4, column=6, pady=(20,0))
    b = Button(mainframe, wraplength=50, text='Reset Total',command=resetTotals, activebackground='yellow', width=10, height=1,  font = ('Sans','10'), padx=10, pady=10)
    b.grid(row=5, column=6, pady=(20,0))

    ## Job Entry ##
    b = Button(mainframe, wraplength=70, text='Scan Barcode',command=scanBarcode, activebackground='yellow', width=10, height=1,  font = ('Sans','10'), padx=10, pady=10)
    b.grid(row=4, column=1, pady=(20,0))
    b = Button(mainframe, wraplength=50, text='Manual Barcode',command=manualBarcode, activebackground='yellow', width=10, height=1,  font = ('Sans','10'), padx=10, pady=10)
    b.grid(row=5, column=1, pady=(20,0))

    txttotalbatch.set(str(gn.batchA+gn.batchB))
    txttotaltotal.set(str(gn.totalA+gn.totalB))

    #monitorGPIO()
    #createConfig()
    root.after(1,countAside())
        

    root.mainloop()

if __name__=="__main__":
    gui()
    main()
Problem: what i am trying to achieve is to get tkinter to update continually to reflect any changes in the undelying data, ie if the machine counts then this is reflected on the GUI.
What i am looking for is a method to trigger a sub in tkinter module from outside the module like GUI.gui.countAside()

Any suggestions greatly appreciated and apologies for my lack of coding skills, i appreciate this can be wrapped alot smaller in the GUI module

Simon
Spend a little time and watch this video:
You'll enjoy it, and learn a lot about concurrent processing
Thanks, homework for tonight!
Just hope its easy enough for me!