I tried to convert a function to a module in tkinter; however, it failed.
Original version: embed a function (sl_adder) in main codes
Here I use .get()
function
from tkinter import *
import tkinter.ttk as ttk
def sl_adder():
aa = float(xxx.get())
bb = float(yyy.get())
cc = aa bb
mylabel = ttk.Label(root, text = cc).grid(column=1, row=4, padx=10, pady=15, sticky=N)
root = Tk()
root.title('Denny')
root.geometry("800x900")
xxx = StringVar()
yyy = StringVar()
ttk.Label(root,text = "Adding System", font=('Arial', 25)).grid(column=0, row=0, ipadx=5, pady=5, sticky=W N)
ttk.Label(root,text = "Input Two Numbers", font=('Arial', 10)).grid(column=0, row=1, ipadx=5, pady=20, sticky=W N)
ttk.Label(root,text = "1st number: ", font=('Arial', 10)).grid(column=0, row=2, ipadx=5, pady=15, sticky=W N)
ttk.Label(root,text = "2nd number: ", font=('Arial', 10)).grid(column=0, row=3, ipadx=5, pady=15, sticky=W N)
ttk.Entry(root, textvariable=xxx, width=20).grid(column=1, row=2, padx=10, pady=15, sticky=N)
ttk.Entry(root, textvariable=yyy, width=20).grid(column=1, row=3, padx=10, pady=15, sticky=N)
resultButton = ttk.Button(root, text = 'Calculate', command = sl_adder)
resultButton.grid(column=3, row=2, pady=15, sticky=W)
close_window = ttk.Button(root, text="Destroy", command = root.destroy)
close_window.grid(column=3, row=3, pady=15, sticky=W)
Second version: convert a function to a module (sl_adder)
from tkinter import *
import tkinter.ttk as ttk
from sl_adder import sl_adder # <-------------------------
root = Tk()
root.title('Denny')
root.geometry("800x900")
xxx = StringVar()
yyy = StringVar()
ttk.Label(root,text = "Adding System", font=('Arial', 25)).grid(column=0, row=0, ipadx=5, pady=5, sticky=W N)
ttk.Label(root,text = "Input Two Numbers", font=('Arial', 10)).grid(column=0, row=1, ipadx=5, pady=20, sticky=W N)
ttk.Label(root,text = "1st number: ", font=('Arial', 10)).grid(column=0, row=2, ipadx=5, pady=15, sticky=W N)
ttk.Label(root,text = "2nd number: ", font=('Arial', 10)).grid(column=0, row=3, ipadx=5, pady=15, sticky=W N)
ttk.Entry(root, textvariable=xxx, width=20).grid(column=1, row=2, padx=10, pady=15, sticky=N)
ttk.Entry(root, textvariable=yyy, width=20).grid(column=1, row=3, padx=10, pady=15, sticky=N)
resultButton = ttk.Button(root, text = 'Calculate', command = sl_adder)
resultButton.grid(column=3, row=2, pady=15, sticky=W)
close_window = ttk.Button(root, text="Destroy", command = root.destroy)
close_window.grid(column=3, row=3, pady=15, sticky=W)
Module sl_adder
:
def sl_adder():
aa = float(xxx.get())
bb = float(yyy.get())
cc = aa bb
mylabel = ttk.Label(root, text = cc).grid(column=1, row=4, padx=10, pady=15, sticky=N)
When I ran it, it shows:
NameError: name 'xxx' is not defined
How to fix it? I only remove the function and produce a module.
============================================================
Now I fix that as following
from tkinter import *
import tkinter.ttk as ttk
from sl_adder import sl_adder
root = Tk()
root.title('Denny')
root.geometry("800x900")
xxx = StringVar()
yyy = StringVar()
ttk.Label(root,text = "Adding System", font=('Arial', 25)).grid(column=0, row=0, ipadx=5, pady=5, sticky=W N)
ttk.Label(root,text = "Input Two Numbers", font=('Arial', 10)).grid(column=0, row=1, ipadx=5, pady=20, sticky=W N)
ttk.Label(root,text = "1st number: ", font=('Arial', 10)).grid(column=0, row=2, ipadx=5, pady=15, sticky=W N)
ttk.Label(root,text = "2nd number: ", font=('Arial', 10)).grid(column=0, row=3, ipadx=5, pady=15, sticky=W N)
ttk.Entry(root, textvariable=xxx, width=20).grid(column=1, row=2, padx=10, pady=15, sticky=N)
ttk.Entry(root, textvariable=yyy, width=20).grid(column=1, row=3, padx=10, pady=15, sticky=N)
aa = float(xxx.get()) # <=====================
bb = float(yyy.get()) # <=====================
resultButton = ttk.Button(root, text = 'Calculate', command = sl_adder(aa,bb))
resultButton.grid(column=3, row=2, pady=15, sticky=W)
close_window = ttk.Button(root, text="Destroy", command = root.destroy)
close_window.grid(column=3, row=3, pady=15, sticky=W)
root.mainloop()
With module
def sl_adder(aa,bb):
cc = aa bb
mylabel = ttk.Label(root, text = cc).grid(column=1, row=4, padx=10, pady=15, sticky=N)
However, another error pops out
aa = float(xxx.get())
ValueError: could not convert string to float: ''
How to fix it? [1]: https://i.stack.imgur.com/xWnyL.jpg
CodePudding user response:
Since main application and module are in different workspaces, you cannot access global variables in main application directly inside module. However you can pass them via function arguments.
Also it is better to create the result label in main application instead of inside module function. Main application can get the result from the module function and update the result text.
Main application:
from tkinter import *
import tkinter.ttk as ttk
from sl_adder import sl_adder
root = Tk()
root.title('Denny')
root.geometry("800x900")
xxx = StringVar()
yyy = StringVar()
ttk.Label(root,text = "Adding System", font=('Arial', 25)).grid(column=0, row=0, ipadx=5, pady=5, sticky=W N)
ttk.Label(root,text = "Input Two Numbers", font=('Arial', 10)).grid(column=0, row=1, ipadx=5, pady=20, sticky=W N)
ttk.Label(root,text = "1st number: ", font=('Arial', 10)).grid(column=0, row=2, ipadx=5, pady=15, sticky=W N)
ttk.Label(root,text = "2nd number: ", font=('Arial', 10)).grid(column=0, row=3, ipadx=5, pady=15, sticky=W N)
ttk.Entry(root, textvariable=xxx, width=20).grid(column=1, row=2, padx=10, pady=15, sticky=N)
ttk.Entry(root, textvariable=yyy, width=20).grid(column=1, row=3, padx=10, pady=15, sticky=N)
resultButton = ttk.Button(root, text = 'Calculate',
# update result label with the returned result
command = lambda: mylabel.config(text=sl_adder(xxx.get(), yyy.get())))
resultButton.grid(column=3, row=2, pady=15, sticky=W)
close_window = ttk.Button(root, text="Destroy", command = root.destroy)
close_window.grid(column=3, row=3, pady=15, sticky=W)
# create the label for showing the result
mylabel = ttk.Label(root)
mylabel.grid(column=1, row=4, padx=10, pady=15, sticky=N)
root.mainloop()
module sl_adder
:
def sl_adder(aa, bb):
# return the result to caller
return float(aa) float(bb)