0

I am trying to make a GUI for already functional python modules as a project. I am new to Tkinter so i used https://stackoverflow.com/a/7557028/10315872 this answer by Bryan Oakley here as a reference for my work with minor changes here and there. The GUI python file here is supposed to get the data from the user and use other python modules PdbHandler and CrdHandler to generate data files which have to be displayed using Tkinter. Attached below is the current progress of the project which is showing an error Attribute error 'Master' object has no attribute '_loadtk'.

  File "C:\Users\Dark12Arrow\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 2022, in __init__
self._loadtk()
AttributeError: 'master' object has no attribute '_loadtk'

I tried using root=tk.Tk() and the passing root to master as was a similar problem in this query Python Tkinter error object has no attribute .But that shows another error.

 File "C:\Users\Dark12Arrow\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 2020, in __init__
self.tk = _tkinter.create(screenName, baseName, className, interactive, 
wantobjects, useTk, sync, use)
TypeError: create() argument 1 must be str or None, not Tk

Since the code is still incomplete page two is only a blank page:

import tkinter as tk
import PdbHandler 
import CrdHandler
from tkinter import filedialog
filetype=0
class master(tk.Frame):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        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.frames = {}
        for F in (StartPage, PageOne, PageTwo):
            page_name = F.__name__
            frame = F(parent=container, controller=self)
            self.frames[page_name] = frame
            self.frames[page_name].grid(row=0, column=0, sticky="nsew")

        self.show_frame("StartPage")

    def show_frame(self, page_name):
        frame = self.frames[page_name]
        frame.tkraise()


class StartPage(tk.Frame):
    def __init__(self,parent,controller):
        tk.Frame.__init__(self,parent)
        def NextAction(event):
            if choice.get()==0:
                print("PDB selected")
                filetype=0
                controller.show_frame(PageOne)
            elif choice.get()==1:
                print("CRD Selected")
                filetype = 1
                controller.show_frame(PageOne)
            else:
                print("select one atleast")

        chosemsg=tk.StringVar()
        choice=tk.IntVar()
        clabel=tk.Label(self,textvariable=chosemsg)
        clabel.grid(row=0,padx=30,pady=10)
        chosemsg.set("Chose File type :")
        pdbradbutton=tk.Radiobutton(self,text="PDB",variable=choice,value=0)
        pdbradbutton.grid(row=1)
        crdradbutton=tk.Radiobutton(self,text="CRD",variable=choice,value=1)
        crdradbutton.grid(row=2)
        nextBttn=tk.Button(self,text="Next", command=lambda: NextAction)
        nextBttn.grid(row=4,pady=10,column=5)

class PageOne(tk.Frame):
    def __init__(self,parent,controller):
        tk.Frame.__init__(self,parent)
        def OpenAction():
            if filetype==0:

                self.filename = filedialog.askopenfilename(initialdir="C:\\", title="Select file",
                                                       filetypes=(("pdb files", "*.pdb"), ("all files", "*.*")))
                PdbHandler.pdbHandler(self.filename)
                controller.show_frame(PageTwo)
            elif filetype==1:
                self.filename = filedialog.askopenfilename(initialdir="C:\\", title="Select file",
                                                       filetypes=(("pdb files", "*.pdb"), ("all files", "*.*")))
                CrdHandler.crdHandler(self.filename)
                controller.show_frame(PageTwo)
            else:
                print("select one atleast")


        filename=tk.StringVar()
        choice=tk.IntVar()
        clabel=tk.Label(self,textvariable="Choose file")
        clabel.grid(row=0,padx=30,pady=10)

        nextBttn=tk.Button(self,text="Open", command=lambda: OpenAction)
        nextBttn.grid(row=4,pady=10,column=5)

class PageTwo(tk.Frame):
    def __init__(self,parent,controller):
        tk.Frame.__init__(self,parent)

app= master()
app.mainloop()
  • Unrelated to the question that was asked, `command=lambda: NextAction` needs to be `command=NextAction` – Bryan Oakley Sep 05 '18 at 11:48
  • I fixed it myself recently but thanks again. On side note ,unrelated to above question, even after fixing the button i had a KeyError : which i fixed by using https://stackoverflow.com/a/32716539/10315872 (also by u). It was not required i guess since in the code that i am using already used the for loop to instantiate all the pages. But it got solved anyhow. The code is running. Starting page is loading. The next button being clicked is also logged into the console using "PDB Selected " or "CRD selected" . But the problem is :next page i.e. PageOne is not loading – Dark Arrow Sep 05 '18 at 12:20
  • If you look at the code you'll see that you need to pass `"PageOne"`, not `PageOne`. It expects the class _name_, not the actual class. – Bryan Oakley Sep 05 '18 at 12:38
  • Sorry to trouble you again but upon making that change i am getting another KeyError : 'PageOne' – Dark Arrow Sep 05 '18 at 13:04
  • It's the very same problem. This is exactly why you shouldn't copy code from the internet without trying to understand it. This is a very simple problem to solve. – Bryan Oakley Sep 05 '18 at 13:14
  • Well no problem there. I fixed it myself. BTW the error, it seems was not the same despite the same key-error exception occurring. extra pair of parenthesis was the error this time around. – Dark Arrow Sep 05 '18 at 15:13

2 Answers2

0

Shouldn't line 8 be:

super().__init__(*args, **kwargs)
Mario Camilleri
  • 1,457
  • 11
  • 24
0

The problem is in these three lines:

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

You are inheriting from tk.Frame, but then trying to call the __init__ method of tk.Tk The first and third lines of the above snippet need to agree on the class.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685