Untitled

mail@pastecode.io avatar
unknown
plain_text
3 years ago
6.7 kB
2
Indexable
import tkinter as tk
from tkinter import ttk


class Model:
    def __init__(self, **kwargs):
        for key in kwargs:
            setattr(self, key, kwargs[key])


class Root(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        # setting window title and icon
        self.title("TCR Tool V2.1")
        self.iconbitmap('D:/Users/Gabriel/Documents/PycharmProjects/pythonProject/Tkinter/Not TCR/Icon/icon.ico')
        self.geometry('600x300')
        self.resizable(width=False, height=False)

        # setting GUI theme
        style = ttk.Style(self)
        style.configure("Button", background="red")
        style.map('Button', background=[('active', 'red')])
        # Import the tcl file
        self.tk.call('source', 'azure/azure.tcl')
        style.theme_use('azure')

        # create Model object for storing user credentials
        self.credentials = Model()

        # create Model object for storing fixed version in nested dictionary
        self.fv_dict = Model()

        # creating a container
        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)

        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)
        self.container = container
        self.show_frame(LoginPage)

    # to display the current frame passed as
    # parameter
    def show_frame(self, cont):
        frame = cont(self.container, self)
        frame.grid(row=0, column=0, sticky="nsew")
        frame.tkraise()


# first window frame startpage
class LoginPage(tk.Frame):
    # Parent is tk.Frame, controller is root
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        self.controller = controller
        self.credentials = controller.credentials
        self.fv_dict = controller.fv_dict

        # configure rows
        self.grid_rowconfigure(0, weight=1)
        self.grid_rowconfigure(1, weight=1)
        self.grid_rowconfigure(2, weight=1)
        self.grid_rowconfigure(3, weight=1)
        self.grid_rowconfigure(4, weight=2)

        # configure column
        self.columnconfigure(0, weight=1)
        self.columnconfigure(1, weight=1)
        self.columnconfigure(2, weight=1)
        self.columnconfigure(3, weight=1)

        # layout for JIRA login
        label_jira_id = ttk.Label(self, text="JIRA Username")
        label_jira_id.grid(row=1, column=0)
        label_jira_pw = ttk.Label(self, text="JIRA Password")
        label_jira_pw.grid(row=2, column=0)

        self.entry_jira_id = ttk.Entry(self)
        self.entry_jira_id.grid(row=1, column=1)
        self.entry_jira_pw = ttk.Entry(self, show="*")
        self.entry_jira_pw.grid(row=2, column=1)

        button_jira_login = ttk.Button(self, text="Login",
                                       command=self.jira_login_clicked)
        button_jira_login.grid(row=3, column=1)

        # Layout for Postgres login
        label_post_id = ttk.Label(self, text="Postgres Username")
        label_post_id.grid(row=1, column=2)
        label_post_pw = ttk.Label(self, text="Postgres Password")
        label_post_pw.grid(row=2, column=2)

        self.entry_post_id = ttk.Entry(self)
        self.entry_post_id.grid(row=1, column=3)
        self.entry_post_pw = ttk.Entry(self, show="*")
        self.entry_post_pw.grid(row=2, column=3)

        button_post_login = ttk.Button(self, text="Login",
                                       command=self.post_login_clicked)
        button_post_login.grid(row=3, column=3)

    def jira_login_clicked(self):
        self.credentials.jira_id = self.entry_jira_id.get()
        self.credentials.jira_pw = self.entry_jira_pw.get()
        # Login to JIRA and check credentials

    def post_login_clicked(self):
        self.credentials.post_id = self.entry_post_id.get()
        self.credentials.post_pw = self.entry_post_pw.get()
        # Query Post and generate nested_dict
        self.fv_dict.content = self.generate_fv_dict()

        # Show next window
        self.controller.show_frame(Page1)

    def generate_fv_dict(self):
        # INSERT QUERY CONVERSION FUNCTION HERE, RIGHT NOW HARD CODED
        return {
            'FWVI': {'Firmware Version': ['9.1.2.0', '9.1.3.0'],
                     'Phoenix Version': ['5.1.2.0', '5.1.3.0']},
            'FWV': {'Fixed Version': ['8.1.2.0', '8.1.3.0'],
                    'Eclipse Version': ['4.1.2.0', '4.1.3.0']}
        }


# second window frame page1
class Page1(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        # passing on values from controller
        self.credentials = controller.credentials
        self.fv_dict = controller.fv_dict

        # defining variable type for combination box
        self.selected_project = tk.StringVar()
        self.selected_fv = tk.StringVar()
        self.selected_fv_number = tk.StringVar()

        # creating interlinked combination box
        # creating project combination box
        self.cb_project = ttk.Combobox(self, values=list(self.fv_dict.content.keys()), width=15,
                                       textvariable=self.selected_project, state='readonly')
        self.cb_project.grid(row=1, column=1)
        self.selected_project.trace('w', self.update_fv)  # track the change event

        # creating fixed version combination box
        self.cb_fv = ttk.Combobox(self, values=None, width=15, textvariable=self.selected_fv, state='readonly')
        self.cb_fv.grid(row=1, column=2)
        self.selected_fv.trace('w', self.update_fv_number)  # track the change event

        # creating fixed version number combination box
        self.cb_fv_number = ttk.Combobox(self, values=None, width=15, textvariable=self.selected_fv_number,
                                         state='readonly')
        self.cb_fv_number.grid(row=1, column=3)

    def update_fv(self, *args):
        self.cb_fv.set('')  # remove the previous selected option
        self.cb_fv_number.set('')
        self.cb_fv['values'] = list(self.fv_dict.content[self.selected_project.get()])

    def update_fv_number(self, *args):
        if self.selected_fv.get() != '':
            self.cb_fv_number.set('')  # remove the previous selected option
            self.cb_fv_number['values'] = list(self.fv_dict.content[self.selected_project.get()]
                                               [self.selected_fv.get()])


# Driver Code
if __name__ == "__main__":
    app = Root()
    app.mainloop()