2

This question is about programming in Python 2.7.x

I wanted to code a programme where there are two functions exist: one of those is a method to get input from the user, and the other one is to show the input. Both are supposed to be done in GUI. Let's call the first function as GET TEXT function, and the second as SHOW TEXT function; my strategy is to open a GUI, show a text box, and put a button to go to SHOW TEXT function. Then, the first line of the SHOW TEXT function is to close the window opened by the GET TEXT function, get the value of the input text, and print it in another GUI.

So, I tried doing this,

from Tkinter import *
import tkMessageBox

def texttobeenteredhere():
    application = Tk()
    textbox = Text(application)
    textbox.pack()

    submitbutton = Button(application, text="OK", command=showinputtext)
    submitbutton.pack()
    application.mainloop()

def showinputtext():
    application.quit()
    thetext = textbox.get()
    print "You typed", thetext

texttobeenteredhere()

I got errors that I could not comprehend, but I hope you get my idea even though my explanation could be really bad. Please suggest a solution to my problem, where the GET TEXT function and SHOW TEXT function have to exist separately in the code.

EDIT: Thanks Josselin for introducing the syntax class in python. What I actually wanted to say was, I want the programme to open a window to get input from the user, and then close the window, and finally open another window to show the input text. I am honestly new to this, but through my prior knowledge and guessing, I tried to modify the code to meet my expectation.

import Tkinter as tk
global passtext
class application(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.textbox = tk.Text(self)
        self.textbox.pack()
        self.submitbutton = tk.Button(self, text="OK", command=self.showinputtext)
        self.submitbutton.pack()
        self.mainloop()

    def showinputtext(self):
        self.thetext = self.textbox.get("1.0", "end-1c")
        print "You typed:", self.thetext
        self.destroy()

class showtext(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.setthetext = tk.StringVar()
        self.setthetext.set(passtext)
        self.showthetext = tk.Label(self, textvariable=self.setthetext)
        self.showthetext.pack()
        self.submitbutton = tk.Button(self, text="OK", command=self.destroy)
        self.submitbutton.pack()
        self.mainloop()

# Launch the GUI
app = application()
# Access the entered text after closing the GUI
passtext = app.thetext
text = showtext()

My English can sometimes be not understandable, but this question is answered. Thank you very much.

dee cue
  • 983
  • 1
  • 7
  • 23

1 Answers1

0

There are 2 main problems in your code:

  • First, in your showinputtext function, you want to access elements of your GUI, but they are not defined within the scope of the function.
  • Second, when reading the content of a tk.Text widget, the .get() method takes 2 arguments (see this link).

To fix the first problem, the best is to define your application as a class, with an inner function taking the class instance self as input argument, such that application widgets can be called within the function.

Code:

import Tkinter as tk

class application(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.textbox = tk.Text(self)
        self.textbox.pack()
        self.submitbutton = tk.Button(self, text="OK", command=self.showinputtext)
        self.submitbutton.pack()
        self.mainloop()

    def showinputtext(self):
        self.thetext = self.textbox.get("1.0", "end-1c")
        print "You typed:", self.thetext
        self.destroy()

# Launch the GUI
app = application()

# Access the entered text after closing the GUI
print "you entered:", app.thetext
Community
  • 1
  • 1
Josselin
  • 2,593
  • 2
  • 22
  • 35