1

I've looked at several other questions and none of them seem to help with my solution. I think I'm just not very intelligent sadly.

Basic question I know. I decided to learn python and I'm making a basic app with tkinter to learn.

Basically it's an app that stores and displays people's driving licence details (name and expiry date). One of the abilities I want it to have is a name lookup. To begin with, I need to figure out how to put a textbox into my window!

I will post the relevant (well, what I think is relevant!) code below:

class search(tk.Frame):


def __init__(self, parent, controller):
    tk.Frame.__init__(self, parent)
    label = tk.Label(self, text="Enter a name to display that individual's details", font=LARGE_FONT)
    label.pack(pady=10,padx=10)     

    label1 = tk.Label(console, text="Name:").pack()
    searchbox = tk.Entry(console)
    searchbox.pack()


    button1 = tk.Button(self, text="SEARCH", command=lambda: controller.show_frame(main))#not created yet
    button1.pack()

    button2 = tk.Button(self, text="HOME", command=lambda: controller.show_frame(main))
    button2.pack()

and of course at the top I have

import tkinter as tk

When I try and run this I get "typeobject "search" has no attribute 'tk'". It worked fine - the search window would open when I click the relevant button on the home window. Until I tried to add the Entry box.

What am I doing wrong here? I'm an utter newbie so I'm prepared to face my stupidity

Also apologies if the formatting of this question is awful, I'm a newbie posting here as well. Putting everything into correct "code" format is a real pain

Tschallacka
  • 27,901
  • 14
  • 88
  • 133
Benno
  • 51
  • 1
  • 3
  • 7
  • Oh and the indentation below the class is correct in the script, it's just a pain to do on here – Benno Aug 09 '17 at 17:24
  • It's not a pain to do here. It takes just a few seconds. Please take the time to fix it. Move the line with "class" all the way to the left. Select all the code. Click the button that looks like `{}`. – Bryan Oakley Aug 09 '17 at 18:55

2 Answers2

0

I'm guessing you're running into problems since you didn't specify a layout manager and passed console instead of self:

import tkinter as tk

class Search(tk.Frame):
    def __init__(self, parent=None, controller=None):
        tk.Frame.__init__(self, parent)

        self.pack()  # specify layout manager

        label1 = tk.Label(self, text="Enter a name to display that individual's details")
        label1.pack(pady=10, padx=10)

        label2 = tk.Label(self, text="Name:")
        label2.pack()

        searchbox = tk.Entry(self)
        searchbox.pack()

        button1 = tk.Button(self, text="SEARCH", command=lambda: controller.show_frame(main))
        button1.pack()

        button2 = tk.Button(self, text="HOME", command=lambda: controller.show_frame(main))
        button2.pack()

# Just cobble up the rest for example purposes:

main = None

class Controller:
    def show_frame(self, frame=None):
        pass

app = Search(controller=Controller())
app.mainloop()
cdlane
  • 40,441
  • 5
  • 32
  • 81
-1

First of all, using from tkinter import * is a more efficient way of importing Tkinters libraries without having to import specific things when needed. To answer your question though, here is the code for entering a text box.
t1 = Text(self)

To insert text into the text box: t1.insert()

An example of this would be t1.insert(END, 'This is text')

If you haven't got it already, t1 is the variable I'm assigning to the text box, although you can choose whatever variable you want. I highly recommend effbots tutorial on tkinter, I found it extremely useful. Here is the link: http://effbot.org/tkinterbook/tkinter-application-windows.htm

Best of luck!

Liam
  • 120
  • 9
  • 2
    `from ... import *` is considered bad style (https://stackoverflow.com/questions/2386714/why-is-import-bad) and often leads to confusion, especilly amongst beginners (https://stackoverflow.com/questions/45502124/on-import-modules-and-method-names-in-python). – laolux Aug 09 '17 at 18:14
  • Good point Hannebambel, but considering all of tkinter is strictly gui related, it would save time instead of importing specific items. – Liam Aug 09 '17 at 18:59
  • using `import tkinter as tk` is the preferred method as it allows for better comparability between versions of tkinter by reducing the needed changes to the code between versions. – Mike - SMT Aug 09 '17 at 19:22
  • also if you use `import tkinter as tk` you prevent any overlapping imports with other libraries. – Mike - SMT Aug 09 '17 at 19:31