How to do it...

First we will increase the size of our ScrolledText widget, making it larger. Let's increase scrol_w to 40 and scrol_h to 10:

    # Using a scrolled Text control 
scrol_w = 40; scrol_h = 10 # increase sizes
self.scrol = scrolledtext.ScrolledText(mighty, width=scrol_w,
height=scrol_h,
wrap=tk.WORD)
self.scrol.grid(column=0, row=3, sticky='WE', columnspan=3)

When we now run the resulting GUI, the Spinbox widget is center-aligned in relation to the Entry widget above it, which does not look good. We'll change this by left-aligning the widget.

Add sticky='W' to the grid control to left-align the Spinbox widget:

    # Adding a Spinbox widget
self.spin = Spinbox(mighty, values=(1, 2, 4, 42, 100), width=5,
bd=9, command=self._spin)
self.spin.grid(column=0, row=2, sticky='W') # align left

The GUI could still look better, so now we will increase the size of the Entry widget to get a more balanced GUI layout.

Increase the width to 24, as shown in the following code snippet:

    # Adding a Textbox Entry widget
self.name = tk.StringVar()
self.name_entered = ttk.Entry(mighty, width=24,
textvariable=self.name)
self.name_entered.grid(column=0, row=1, sticky='W')

Let's also slightly increase the width of Combobox to 14:

    ttk.Label(mighty, text="Choose a number:").grid(column=1, row=0)
number = tk.StringVar()
self.number_chosen = ttk.Combobox(mighty, width=14,
textvariable=number,
state='readonly')
self.number_chosen['values'] = (1, 2, 4, 42, 100)
self.number_chosen.grid(column=1, row=1)
self.number_chosen.current(0)

Running the modified and improved code results in a larger GUI, which we will use for this and the following recipes:

GUI_multiple_threads.py

In order to create and use threads in Python, we have to import the Thread class from the threading module:

    #======================
# imports
#======================
import tkinter as tk
from tkinter import ttk
from tkinter import scrolledtext
from tkinter import Menu
from tkinter import messagebox as msg
from tkinter import Spinbox
from time import sleep
import Ch06_Code.ToolTip as tt
from threading import Thread

GLOBAL_CONST = 42

Let's add a method to be created in a thread to our OOP class:

    class OOP(): 
def method_in_a_thread(self):
print('Hi, how are you?')

We can now call our threaded method in the code, saving the instance in a variable:

    #======================
# Start GUI
#======================
oop = OOP()

# Running methods in Threads
run_thread = Thread(target=oop.method_in_a_thread)

oop.win.mainloop()

Now, we have a method that is threaded, but when we run the code, nothing gets printed to the console!

We have to start the thread first before it can run, and the next recipe will show us how to do this.

However, setting a breakpoint after the GUI main event loop proves that we did indeed create a thread object, as can be seen in the Eclipse IDE debugger:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.191.86