How to take value of ttk.Entry and display it in a messagebox?

638 views Asked by At

I have just started out with tkinter and python3 in general. I'm using pygubu to try and make a very simple that takes input from an Entry widget and displays it in a Messagebox on the press of a button. Below is the code i'm using:

# command_properties.py
import tkinter as tk
from tkinter import messagebox
import pygubu

# define the function callbacks
def on_button1_click():
    tk_messageBox -message answer -type ok -icon info

class MyApplication(pygubu.TkApplication):

    def _create_ui(self):
        #1: Create a builder
        self.builder = builder = pygubu.Builder()

        #2: Load an ui file
        builder.add_from_file('test.ui')

        #3: Create the widget using self.master as parent
        self.mainwindow = builder.get_object('Frame_1', self.master)

        # Configure callbacks
        callbacks = {
            'on_button1_clicked': on_button1_click
        }

        builder.connect_callbacks(callbacks)


if __name__ == '__main__':
    root = tk.Tk()
    app = MyApplication(root)
    app.run()

Here is the ui file code : http://pastebin.com/puRbD87m Also, please tell me where i'm wrong specifically.

1

There are 1 answers

2
furas On

Create on_button1_click() as method inside class and then you can create

self.entry_1 = self.builder.get_object('Entry_1', self.master)

and access text using

self.entry_1.get()

Full example

import tkinter as tk
import pygubu

class MyApplication(pygubu.TkApplication):

    def _create_ui(self):
        self.builder = pygubu.Builder()

        self.builder.add_from_file('test.ui')

        mainwindow = builder.get_object('mainwindow', master)

        self.entry_1 = self.builder.get_object('Entry_1', self.master) # <- self.entry

        callbacks = {
            'on_button1_clicked': self.on_button1_click # <- self.
        }

        self.builder.connect_callbacks(callbacks)

    def on_button1_click(self): # <- self
        print(self.entry_1.get())

if __name__ == '__main__':
    root = tk.Tk()
    app = MyApplication(root)
    app.run()