How to output the results in an entry widget in tkinter - python

I am struggling with this small application. I fallowed many forum and answer but I couldn't find the right answer.
I marked in the code the problems I am having:
The first problem is I could not find a way for output the results in a ttk entry widget.
The second problem is how to execute different code depending on the radiobutton checked.
The third problem is how can I make the program with better syntax?
Code:
from tkinter import ttk
from tkinter import *
from tkinter import messagebox
class main_window:
def __init__(self, master = None):
self.frame1 = ttk.Frame(master)
self.frame1.pack()
master.title('Calculation program')
master.resizable(False, False)
master.configure(background='blue')
self.radiob1 = ttk.Radiobutton(self.frame1, value="M", text="Molar (M)")
self.radiob1.grid(column=0, row=1, sticky="nw", padx=25)
self.radiob2 = ttk.Radiobutton(self.frame1, value="mM", text="milliMolar (mM)")
self.radiob2.grid(column=0, row=2, sticky="nw", padx=25)
self.mol_weight = ttk.Entry(self.frame1, width=24, font=('Arial', 10))
self.mol_weight.grid(row=0, column=3, padx=3, pady=5)
self.amount = ttk.Entry(self.frame1, width=24, font=('Arial', 10))
self.amount.grid(row=1, column=3, padx=3, pady=5)
self.results = ttk.Entry(self.frame1, width=15, font=('Cambria', 10))
self.results.grid(row=6, column=3, padx=5, pady=5)
ttk.Label(self.frame1, text='Molecular Weight:').grid(row=0, column=1, padx=0, pady=3, sticky='w')
ttk.Label(self.frame1, text='Amount:').grid(row=1, column=1, padx=0, pady=3, sticky='w')
ttk.Label(self.frame1, text='Results').grid(row=5, column=3, padx=0, pady=6, sticky='s')
ttk.Button(self.frame1, text='Calculate',
command=self.calculate).grid(row=4, column=0, padx=5, pady=5, sticky='e')
ttk.Button(self.frame1, text='Clear',
command=self.clear).grid(row=4, column=1, padx=5, pady=5, sticky='w')
def calculate(self):
if self.radiob1.SELECTED??:
return self.molare()
elif self.radiob2.SELECTED??:
return self.millimolar
else:
messagebox.showinfo(title="No good, you have to select one!")
messagebox.showinfo(title='Calculations', message='Calculations Completed!')
def molare(self):
a = self.mol_weight.get()
b = self.amount.get()
ans = a + b
self.results["Results"] = "Is: " + ans [???]
def millimolar(self):
a = self.mol_weight.get()
b = self.amount.get()
ans = a - b
self.results["Results"] = "Is: " + ans [???]
def clear(self):
self.mol_weight.delete(0, 'end')
self.amount.delete(0, 'end')
self.radiob1.DESELECT() ??
self.radiob2.DESELECT()??
def main():
root = Tk()
Main_window = main_window(root)
root.mainloop()
if __name__ == "__main__":
main()

1) To put text in an Entry widget, use the insert method:
entry.delete(0, "end") # clear entry
entry.insert(0, "my text") # insert new text
2) Associate a StringVar with your radiobuttons and use its get method to know which button is selected
import tkinter as tk
from tkinter import ttk
def callback():
if var.get() == "r1":
print("r1 is selected")
elif var.get() == "r2":
print("r2 is selected")
else:
print("None")
root = tk.Tk()
var = tk.StringVar(root, value="")
ttk.Radiobutton(root, variable=var, value="r1", text="r1").pack()
ttk.Radiobutton(root, variable=var, value="r2", text="r2").pack()
ttk.Button(root, text="print", command=callback).pack()
root.mainloop()
To clear the radiobutton selection, you can do var.set("").
3) It's a rather subjective question. First, class name are usually capitalized. Personally, I make my Main class inherit from tk.Tk:
class Main(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
# window configuration
self.title('Calculation program')
#...
# widgets
ttk.Label(self, text="example").grid()
#...
self.mainloop()
if __name__ == "__main__":
Main()
And, as it was said in the comments, a and b should be converted to float or int before making calculations and then do "Is %d" % ans or any of the other proposition of furas in the comments.
I recommend you this website: http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/index.html to get the list of all options and methods of each tkinter/ttk widget.

Related

Getting multi-tabbed tkinter application with buttons to work properly

I have a small test application using Python tkinter that I got to work. The problem is that it does not exit properly when the "Quit" button is pressed. It is a two-frame tabbed application where I started with the StackOverflow question ttk tkinter multiple frames/windows.
It is now a full example that works, but needs work because it doesn't quit and exit properly. When I press the "Quit" button, it kills the frame for that tab, but the application doesn't quit and exit properly. I have to hit the Window "X" Close icon to close it.
My main question is how (and where?) do I test for the event on either the "Quit" button on the "Feet to Meters" calculator, or the "Cancel/Quit" button on the BMI calculator.
A second question I have is that the design of the application seems inefficient to me, because it creates two widgets "Frame" objects, each with their own set of buttons, including 2 "quit" buttons. How do I put these tabs and frames into a parent window and then add a quit button on that parent window to close the entire application.
I modified the buttons to properly destroy the Frame that the button is in:
Changed button2 "command=self.quit" to "command=self.destroy"
self.button2 = ttk.Button(self, text="Cancel/Quit", command=self.quit).grid(row=3, column=1, sticky=E)
to
self.button2 = ttk.Button(self, text="Cancel/Quit", command=self.destroy).grid(row=3, column=1, sticky=E)
""" Created on Thu Jul 11 17:20:22 2019 """
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
class App1(ttk.Frame):
""" This application calculates BMI and returns a value. """
def __init__(self, master=None):
ttk.Frame.__init__(self, master)
self.grid()
self.createWidgets()
def createWidgets(self):
#text variables
self.i_height = StringVar()
self.i_weight = StringVar()
self.o_bmi = StringVar()
#labels
self.label1 = ttk.Label(self, text="Enter your weight:").grid(row=0, column=0, sticky=W)
self.label2 = ttk.Label(self, text="Enter your height:").grid(row=1, column=0, sticky=W)
self.label3 = ttk.Label(self, text="Your BMI is:").grid(row=2, column=0, sticky=W)
#text boxes
self.textbox1 = ttk.Entry(self, textvariable=self.i_weight).grid(row=0, column=1, sticky=E)
self.textbox2 = ttk.Entry(self, textvariable=self.i_height).grid(row=1, column=1, sticky=E)
self.textbox3 = ttk.Entry(self, textvariable=self.o_bmi).grid(row=2, column=1, sticky=E)
#buttons
self.button1 = ttk.Button(self, text="Ok", command=self.calculateBmi).grid(row=3, column=2, sticky=E)
## Changed button2 "command=self.quit" to "command=self.destroy"
# self.button2 = ttk.Button(self, text="Cancel/Quit", command=self.quit).grid(row=3, column=1, sticky=E)
self.button2 = ttk.Button(self, text="Cancel/Quit", command=self.destroy).grid(row=3, column=1, sticky=E)
#exitApplication = tk.Button(root, text='Exit Application', command=root.destroy)
#canvas1.create_window(85, 300, window=exitApplication)
def calculateBmi(self):
try:
self.weight = float(self.i_weight.get())
self.height = float(self.i_height.get())
self.bmi = self.weight / self.height ** 2.0
self.o_bmi.set(self.bmi)
except ValueError:
messagebox.showinfo("Error", "You can only use numbers.")
finally:
self.i_weight.set("")
self.i_height.set("")
class App2(ttk.Frame):
""" Application to convert feet to meters or vice versa. """
def __init__(self, master=None):
ttk.Frame.__init__(self, master)
self.grid()
self.create_widgets()
def create_widgets(self):
"""Create the widgets for the GUI"""
# 1 textbox (stringvar)
self.entry= StringVar()
self.textBox1= ttk.Entry(self, textvariable=self.entry).grid(row=0, column=1)
# 5 labels (3 static, 1 stringvar)
self.displayLabel1 = ttk.Label(self, text="feet").grid(row=0, column=2, sticky=W)
self.displayLabel2 = ttk.Label(self, text="is equivalent to:").grid(row=1, column=0)
self.result= StringVar()
self.displayLabel3 = ttk.Label(self, textvariable=self.result).grid(row=1, column=1)
self.displayLabel4 = ttk.Label(self, text="meters").grid(row=1, column=2, sticky=W)
# 2 buttons
self.calculateButton = ttk.Button(self, text="Calculate", command=self.convert_feet_to_meters).grid(row=2, column=2, sticky=(S,E))
self.quitButton = ttk.Button(self, text="Quit", command=self.destroy).grid(row=2, column=1, sticky=(S,E))
#exitApplication = tk.Button(root, text='Exit Application', command=root.destroy)
#canvas1.create_window(85, 300, window=exitApplication)
def convert_feet_to_meters(self):
"""Converts feet to meters, uses string vars and converts them to floats"""
self.measurement = float(self.entry.get())
self.meters = self.measurement * 0.3048
self.result.set(self.meters)
### CODE BELOW COMMENTED OUT WHEN JOINING ORIGINAL POSTER CODE WITH HIS SOLUTION
### It seems no longer relevant since App1 and App2 have their own buttons.
#def button1_click():
# """ This is for the BMI Calculator Widget """
# root = Tk()
# app = App1(master=root)
# app.mainloop()
#
#def button2_click():
# """ This is for the Feet to Meters Conversion Widget """
# root = Tk()
# app = App2(master=root)
# app.mainloop()
#def main():
# window = Tk()
# button1 = ttk.Button(window, text="bmi calc", command=button1_click).grid(row=0, column=1)
# button2 = ttk.Button(window, text="feet conv", command=button2_click).grid(row=1, column=1)
# window.mainloop()
def main():
#Setup Tk()
window = Tk()
#Setup the notebook (tabs)
notebook = ttk.Notebook(window)
frame1 = ttk.Frame(notebook)
frame2 = ttk.Frame(notebook)
notebook.add(frame1, text="BMI Calc")
notebook.add(frame2, text="Feet to Meters")
notebook.grid()
#Create tab frames
app1 = App1(master=frame1)
app1.grid()
app2 = App2(master=frame2)
app2.grid()
#Main loop
window.mainloop()
if __name__ == '__main__':
main()
The application doesn't quit when the "Quit" button is pressed. Only the individual frames quit.
Thanks to Martineau for the hint that helped me get this example to work. I declared 'window as a global variable, since it was defined in the name space of the class constructors. Without that, there was an error raised of undefined window. This method breaks the encapsulation and modularity of the classes by passing in the window as a global. If there is a better way to do this, I would like to know.
# -*- coding: utf-8 -*-
""" Created on Thu Jul 11 17:20:22 2019
Filename: tkinter-multiple-frames-windows_v3.py
From question on StackOverflow question "ttk tkinter multiple frames/windows"
at https://stackoverflow.com/questions/6035101/ttk-tkinter-multiple-frames-windows?rq=1
Now a full example that works but it needed some modification to clarify how it works.
"""
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
class BMICalcApp(ttk.Frame):
""" This application calculates BMI and returns a value. """
def __init__(self, master=None):
ttk.Frame.__init__(self, master)
self.grid()
self.createWidgets()
def createWidgets(self):
#text variables
self.i_height = StringVar()
self.i_weight = StringVar()
self.o_bmi = StringVar()
#labels
self.label1 = ttk.Label(self, text="Enter your weight:").grid(row=0, column=0, sticky=W)
self.label2 = ttk.Label(self, text="Enter your height:").grid(row=1, column=0, sticky=W)
self.label3 = ttk.Label(self, text="Your BMI is:").grid(row=2, column=0, sticky=W)
#text boxes
self.textbox1 = ttk.Entry(self, textvariable=self.i_weight).grid(row=0, column=1, sticky=E)
self.textbox2 = ttk.Entry(self, textvariable=self.i_height).grid(row=1, column=1, sticky=E)
self.textbox3 = ttk.Entry(self, textvariable=self.o_bmi).grid(row=2, column=1, sticky=E)
#buttons
self.button1 = ttk.Button(self, text="Ok", command=self.calculateBmi).grid(row=3, column=2, sticky=E)
self.button2 = ttk.Button(self, text="Cancel/Quit", command=window.destroy).grid(row=3, column=1, sticky=E)
def calculateBmi(self):
try:
self.weight = float(self.i_weight.get())
self.height = float(self.i_height.get())
self.bmi = self.weight / self.height ** 2.0
self.o_bmi.set(self.bmi)
except ValueError:
messagebox.showinfo("Error", "You can only use numbers.")
finally:
self.i_weight.set("")
self.i_height.set("")
class ConvertFeetMeters(ttk.Frame):
""" Application to convert feet to meters or vice versa. """
def __init__(self, master=None):
ttk.Frame.__init__(self, master)
self.grid()
self.create_widgets()
def create_widgets(self):
"""Create the widgets for the GUI"""
# 1 textbox (stringvar)
self.entry= StringVar()
self.textBox1= ttk.Entry(self, textvariable=self.entry).grid(row=0, column=1)
# 5 labels (3 static, 1 stringvar)
self.displayLabel1 = ttk.Label(self, text="feet").grid(row=0, column=2, sticky=W)
self.displayLabel2 = ttk.Label(self, text="is equivalent to:").grid(row=1, column=0)
self.result= StringVar()
self.displayLabel3 = ttk.Label(self, textvariable=self.result).grid(row=1, column=1)
self.displayLabel4 = ttk.Label(self, text="meters").grid(row=1, column=2, sticky=W)
# 2 buttons
self.calculateButton = ttk.Button(self, text="Calculate", command=self.convert_feet_to_meters).grid(row=2, column=2, sticky=(S,E))
self.quitButton = ttk.Button(self, text="Quit", command=window.destroy).grid(row=2, column=1, sticky=(S,E))
def convert_feet_to_meters(self):
"""Converts feet to meters, uses string vars and converts them to floats"""
self.measurement = float(self.entry.get())
self.meters = self.measurement * 0.3048
self.result.set(self.meters)
def main():
#Setup Tk()
global window
window = Tk()
#Setup the notebook (tabs)
notebook = ttk.Notebook(window)
frame1 = ttk.Frame(notebook)
frame2 = ttk.Frame(notebook)
notebook.add(frame1, text="BMI Calc")
notebook.add(frame2, text="Feet to Meters")
notebook.grid()
#Create tab frames
bmi_calc = BMICalcApp(master=frame1)
bmi_calc.grid()
feet_meters_calc = ConvertFeetMeters(master=frame2)
feet_meters_calc.grid()
#Main loop
window.mainloop()
if __name__ == '__main__':
main()

What can I do to make the frame without widgets?

What I want the frame to do is that when I click on the 'clear' button, the frame is cleaned but it does not and when I enter a string that is not valid and then a valid one, it shows traces of the past and past action. I already tried changing the Label.grid () by a Label.pack (), but it is worse since the 'animation' looks like a stack without removing any element when the 'clear' button is pressed
This is basically what would make it change
from tkinter import *
import tkinter.ttk as ttk
def clear():
area.delete(0,END)
frame.config(bd=1, relief=SUNKEN)
frame.update()
status = Label(frame)
status.grid(row=0, column=0, sticky=NSEW)
def statusVal(value):
if not value == 0:
status = Label(frame, background="#ff4242", fg="#262626", text="Cadena invalida", anchor="center")
status.grid(row=0, column=0)
frame.config(bd=1, relief=SUNKEN, background="#ff4242")
frame.update()
else:
status = Label(frame, background="#56ed42", fg="#262626", text="Cadena valida", anchor="center")
status.grid(row=0, column=0)
frame.config(bd=1, relief=SUNKEN, background="#56ed42")
frame.update()
#Test
def validation():
capture = area.get()
if capture == '1':
return statusVal(0)
else:
return statusVal(1)
root = Tk()
root.geometry("300x150+300+300")
area = Entry(root)
area.grid(row=1, column=0, columnspan=2, sticky=E+W+S+N, padx=5)
frame = Frame(root, bd=1, relief=SUNKEN)
frame.grid(row=2, column=0, padx=5, pady=5, columnspan=2, sticky=W+E+S+N)
frame.columnconfigure(0,weight=5)
frame.rowconfigure(0,weight=5)
abtn = Button(root, text="Validate", command=validation)
abtn.grid(row=1, column=3)
cbtn = Button(root, text="Clear", command=clear)
cbtn.grid(row=2, column=3, pady=5)
root.mainloop()
See if this works better. The main change was to have the status Label always exist and hide or unhide it as desired — instead of creating a new one every time the validation() function was called. I also removed the code that was explicitly updating the frame which isn't necessary.
from tkinter import *
import tkinter.ttk as ttk
def clear():
area.delete(0,END)
status.grid_remove() # Hide. but remember grid options.
def statusVal(value):
if not value == 0:
status.config(background="#ff4242", fg="#262626", text="Cadena invalida",
anchor="center")
status.grid() # Unhide
else:
status.config(background="#56ed42", fg="#262626", text="Cadena valida",
anchor="center")
status.grid() # Unhide
#Test
def validation():
capture = area.get()
if capture == '1':
statusVal(0)
else:
statusVal(1)
# Main
root = Tk()
root.geometry("300x150+300+300")
area = Entry(root)
area.grid(row=1, column=0, columnspan=2, sticky=E+W+S+N, padx=5)
frame = Frame(root, bd=1, relief=SUNKEN)
frame.grid(row=2, column=0, padx=5, pady=5, columnspan=2, sticky=W+E+S+N)
frame.columnconfigure(0,weight=5)
frame.rowconfigure(0,weight=5)
# Initialize status Label.
status = Label(frame, anchor="center")
status.grid(row=0, column=0)
status.grid_remove() # Hide it.
abtn = Button(root, text="Validate", command=validation)
abtn.grid(row=1, column=3)
cbtn = Button(root, text="Clear", command=clear)
cbtn.grid(row=2, column=3, pady=5)
root.mainloop()

Triggering a button click with the enter key in Python

Please forgive me in advance, as I am very new to Python. I'm putting together this code for a very simple rate calculator and want to make it where when the Enter key is pressed the program calculates the total (executes the calculate command). Python returns an error saying that Calculate is not defined, however I've defined it several lines above. Any ideas?
import tkinter
from tkinter import ttk
from tkinter import *
root = Tk()
root.withdraw()
class Adder(ttk.Frame):
"""The adders gui and functions."""
def __init__(self, parent, *args, **kwargs):
ttk.Frame.__init__(self, parent, *args, **kwargs)
self.root = parent
self.init_gui()
def on_quit(self):
"""Exits program."""
quit()
def calculate(self, *args):
"""Calculates the sum of the two inputted numbers."""
num1 = int(self.num1_entry.get())
num2 = float(self.num2_entry.get())
num3 = ((num1/2000) * num2)
self.answer_label['text'] = num3
def init_gui(self):
self.root.title('Rate Calculator')
self.root.option_add('*tearOff', 'FALSE')
self.grid(column=0, row=0, sticky='nsew')
self.menubar = tkinter.Menu(self.root)
self.menu_file = tkinter.Menu(self.menubar)
self.menu_file.add_command(label='Exit', command=self.on_quit)
self.menu_edit = tkinter.Menu(self.menubar)
self.menubar.add_cascade(menu=self.menu_file, label='File')
self.menubar.add_cascade(menu=self.menu_edit, label='Edit')
self.root.config(menu=self.menubar)
self.num1_entry = ttk.Entry(self, width=5)
self.num1_entry.grid(column=1, row = 2)
self.num2_entry = ttk.Entry(self, width=5)
self.num2_entry.grid(column=3, row=2)
self.calc_button = ttk.Button(self, text='Calculate',
command=self.calculate)
self.calc_button.grid(column=0, row=3, columnspan=4)
self.answer_frame = ttk.LabelFrame(self, text='Answer',
height=100)
self.answer_frame.grid(column=0, row=4, columnspan=4, sticky='nesw')
self.answer_label = ttk.Label(self.answer_frame, text='')
self.answer_label.grid(column=0, row=0)
# Labels that remain constant throughout execution.
ttk.Label(self, text='Rate Calculator').grid(column=0, row=0,
columnspan=4)
ttk.Label(self, text='Weight').grid(column=0, row=2,
sticky='w')
ttk.Label(self, text='Rate').grid(column=2, row=2,
sticky='w')
ttk.Separator(self, orient='horizontal').grid(column=0,
row=1, columnspan=4, sticky='ew')
for child in self.winfo_children():
child.grid_configure(padx=5, pady=5)
root.bind('<Return>', calculate)
if __name__ == '__main__':
root = tkinter.Tk()
Adder(root)
root.mainloop()
I've reorganised your code slightly so it now calls the calculate method either when you press the button or when you hit Enter.
import tkinter as tk
from tkinter import ttk
class Adder(ttk.Frame):
"""The adders gui and functions."""
def __init__(self, parent, *args, **kwargs):
ttk.Frame.__init__(self, parent, *args, **kwargs)
self.root = parent
self.init_gui()
def on_quit(self):
"""Exits program."""
tk.quit()
def calculate(self, *args):
"""Calculates the sum of the two inputted numbers."""
num1 = int(self.num1_entry.get())
num2 = float(self.num2_entry.get())
num3 = ((num1/2000) * num2)
self.answer_label['text'] = num3
def init_gui(self):
self.root.title('Breakbulk Rate Calculator')
self.root.option_add('*tearOff', 'FALSE')
self.grid(column=0, row=0, sticky='nsew')
self.menubar = tk.Menu(self.root)
self.menu_file = tk.Menu(self.menubar)
self.menu_file.add_command(label='Exit', command=self.on_quit)
self.menu_edit = tk.Menu(self.menubar)
self.menubar.add_cascade(menu=self.menu_file, label='File')
self.menubar.add_cascade(menu=self.menu_edit, label='Edit')
self.root.config(menu=self.menubar)
self.num1_entry = ttk.Entry(self, width=5)
self.num1_entry.grid(column=1, row = 2)
self.num2_entry = ttk.Entry(self, width=5)
self.num2_entry.grid(column=3, row=2)
self.calc_button = ttk.Button(self, text='Calculate',
command=self.calculate)
self.calc_button.grid(column=0, row=3, columnspan=4)
self.answer_frame = ttk.LabelFrame(self, text='Answer',
height=100)
self.answer_frame.grid(column=0, row=4, columnspan=4, sticky='nesw')
self.answer_label = ttk.Label(self.answer_frame, text='')
self.answer_label.grid(column=0, row=0)
# Labels that remain constant throughout execution.
ttk.Label(self, text='Breakbulk Rate Calculator').grid(column=0, row=0,
columnspan=4)
ttk.Label(self, text='Weight').grid(column=0, row=2,
sticky='w')
ttk.Label(self, text='Rate').grid(column=2, row=2,
sticky='w')
ttk.Separator(self, orient='horizontal').grid(column=0,
row=1, columnspan=4, sticky='ew')
for child in self.winfo_children():
child.grid_configure(padx=5, pady=5)
self.root.bind('<Return>', lambda event: self.calculate())
if __name__ == '__main__':
root = tk.Tk()
Adder(root)
root.mainloop()
To create a callback with the proper signature for .bind I've used a lambda function that simply discards the Event object that gets passed to it.
I also got rid of the evil from tkinter import * wildcard import.
The problem is coming from this part of your code
root.bind('<Return>', calculate)
change it to
root.bind('<Return>', self.calculate)
it will work.

Tkinter entry insert from another class

I am learning a bit of tkinter at the moment and have come across a problem I can't seem to solve.
I have 1 class (named 'GUI') in which I first declare an object of another class (named 'client') then create a simple tkinter GUI with an entry field in which the user enters an address.
I then have a button that when pressed, gets the address and passes it to a function of the client class. This function calls another function from a library, which goes out to an external source (the 'client') and gets the value of a boolean register at that address.
Up to here I have working.
I now would like to update another entry field in the tkinter GUI with the value of that Boolean register.
Here is a code sample:
class GUI:
def __init__(self, master):
device = client()
self.master = master
#Define size (x,y) and position (top left is 0,0) of window
master.geometry("800x400+300+300")
#Disable window resize
master.resizable(width=False, height=False)
#GUI Controls
lbl_read_discrete_output = Label(self.master, text="Read boolean register")
lbl_read_discrete_output_address = Label(self.master, text="Address:")
ent_read_discrete_output_address = Entry(self.master)
btn_read_discrete_output_address = Button(self.master, text='READ', command=lambda: device.read_discrete_output(int(ent_read_discrete_output_address.get())))
ent_read_discrete_output_value = Entry(self.master, state='disabled')
#GUI Layout
lbl_read_discrete_output.grid(column=0, row=6, padx=5, pady=5)
lbl_read_discrete_output_address.grid(column=0, row=7, padx=5, pady=5)
ent_read_discrete_output_address.grid(column=1, row=7, padx=5, pady=5)
btn_read_discrete_output_address.grid(column=2, row=7, padx=5, pady=5)
ent_read_discrete_output_value.grid(column=3, row=7, padx=5, pady=5)
class client:
def __init__(self):
pass
def read_discrete_output(self, address):
data = self._client.read_coils(address-1, 1).bits[0:1]
if not data[0]:
#Update entry field 'ent_read_discrete_output_value' with "FALSE"
else:
#Update entry field 'ent_read_discrete_output_value' with "TRUE"
def main():
#Create GUI object
root = Tk()
app = GUI(root)
root.mainloop()
if __name__ == '__main__':
main()
Any help would be greatly appreciated!
You can solve that problem, this way:
import tkinter as tk
from tkinter import *
from pymodbus.client.sync import ModbusTcpClient as ModbusClient
class GUI:
def __init__(self, master):
self.master = master
#GUI Controls
lbl_read_discrete_output = Label(self.master, text="Read boolean register")
lbl_read_discrete_output_address = Label(self.master, text="Address:")
self.ent_read_discrete_output_address = Entry(self.master)
btn_read_discrete_output_address = Button(self.master, text='READ', command=lambda: device.read_discrete_output(int(self.ent_read_discrete_output_address.get())))
self.ent_read_discrete_output_value = Entry(self.master, state='disabled')
#GUI Layout
lbl_read_discrete_output.grid(column=0, row=6, padx=5, pady=5)
lbl_read_discrete_output_address.grid(column=0, row=7, padx=5, pady=5)
self.ent_read_discrete_output_address.grid(column=1, row=7, padx=5, pady=5)
btn_read_discrete_output_address.grid(column=2, row=7, padx=5, pady=5)
self.ent_read_discrete_output_value.grid(column=3, row=7, padx=5, pady=5)
self.x=self.ent_read_discrete_output_address.get()
print(self.x)
device=clientx(self.ent_read_discrete_output_value,self.ent_read_discrete_output_address.get())
class clientx():
def __init__(self,ent,address):
self.address=address
self.ent=ent
def read_discrete_output(self, address):
with ModbusClient('127.0.0.1') as client:
data = self.client.read_coils(address-1, 1).bits[0:1]
if not data[0]:
self.ent.config(state="disabled")
else:
self.ent.config(state="disabled")
def main():
#Create GUI object
root = tk.Tk()
app = GUI(root)
root.mainloop()
if __name__ == '__main__':
main()
Let me know if this works.

How to use tkinter's widget.config() to modify separate widgets that were created by the same method?

I'm pretty new to Python. I can MAKE things work, but I'm never really sure if I'm actually using best practices, so forgive me if I'm not explaining things correctly, or if my code isn't written using best practices. I'm working on it!
I'm trying to figure out how to use tkinter's widget.config() to modify separate widgets that were created by the same method? For example, in the code below, how can I use widget.config() to modify the text in results section 2, r2??
from tkinter import *
from tkinter.ttk import *
class App():
def __init__(self, master):
self.master = master
self.master.title('Question')
self.create_main_frame()
def create_main_frame(self):
self.main_f = Frame(self.master)
nb = Notebook(padding=5)
nb.pack(anchor='w')
self.tab1(nb)
def tab1(self, nb):
tab1_frame = Frame(nb)
nb.add(tab1_frame, text='Tab 1', underline=0)
prod_lab = Label(tab1_frame, text='Entry:')
prod_lab.grid(row=1, column=0, padx=(10,0), pady=(10,5), sticky='e')
self.product_e = Entry(tab1_frame, width=40)
self.product_e.grid(row=1, column=1, padx=(0,10), pady=(10,5))
self.button1 = Button(tab1_frame,
text="Run",
command=self.run1
)
self.button1.grid(row=2, column=1, padx=(0,10), pady=(0,10), sticky='ne')
self.results1 = self.create_result(tab1_frame, 'Results Section 1')
self.results2 = self.create_result(tab1_frame, 'Results Section 2')
self.results3 = self.create_result(tab1_frame, 'Results Section 3')
def create_result(self, frame, name):
lab_frame = LabelFrame(master=frame, text=name)
lab_frame.grid(sticky='we', padx=10, pady=10, columnspan=10)
l1 = Label(master=lab_frame, text='Result 1:')
l1.grid(sticky='e', row=0, column=0, pady=2)
l2 = Label(master=lab_frame, text='Result 2:')
l2.grid(sticky='e', row=1, column=0, pady=2)
l3 = Label(master=lab_frame, text='Result 3:')
l3.grid(sticky='e', row=2, column=0, pady=2)
r1 = Label(master=lab_frame, text='')
r1.grid(sticky='w', row=0, column=1, pady=2)
r2 = Label(master=lab_frame, text='')
r2.grid(sticky='w', row=1, column=1, pady=2)
r3 = Label(master=lab_frame, text='')
r3.grid(sticky='w', row=2, column=1, pady=2)
def run1(self):
#This is the method that will be used to find the results I need
#For the sake of this question, let's pretend I want to put whatever text is in Entry 1, into results section 2 result 2
print (self.product_e.get())
print ('How do I use "widget.config()" to modify the 3 results sections separately???')
master = Tk()
app = App(master)
master.mainloop()
Thanks so much in advance for your help!
I'm not good in Tkinter so I would change create_result() to class Results.
from Tkinter import *
from ttk import *
class Results():
def __init__(self, frame, name):
self.lab_frame = LabelFrame(master=frame, text=name)
self.lab_frame.grid(sticky='we', padx=10, pady=10, columnspan=10)
self.l1 = Label(master=self.lab_frame, text='Result 1:')
self.l1.grid(sticky='e', row=0, column=0, pady=2)
self.l2 = Label(master=self.lab_frame, text='Result 2:')
self.l2.grid(sticky='e', row=1, column=0, pady=2)
self.l3 = Label(master=self.lab_frame, text='Result 3:')
self.l3.grid(sticky='e', row=2, column=0, pady=2)
self.r1 = Label(master=self.lab_frame, text='')
self.r1.grid(sticky='w', row=0, column=1, pady=2)
self.r2 = Label(master=self.lab_frame, text='')
self.r2.grid(sticky='w', row=1, column=1, pady=2)
self.r3 = Label(master=self.lab_frame, text='')
self.r3.grid(sticky='w', row=2, column=1, pady=2)
class App():
def __init__(self, master):
self.master = master
self.master.title('Question')
self.create_main_frame()
def create_main_frame(self):
self.main_f = Frame(self.master)
nb = Notebook(padding=5)
nb.pack(anchor='w')
self.tab1(nb)
def tab1(self, nb):
tab1_frame = Frame(nb)
nb.add(tab1_frame, text='Tab 1', underline=0)
prod_lab = Label(tab1_frame, text='Entry:')
prod_lab.grid(row=1, column=0, padx=(10,0), pady=(10,5), sticky='e')
self.product_e = Entry(tab1_frame, width=40)
self.product_e.grid(row=1, column=1, padx=(0,10), pady=(10,5))
self.button1 = Button(tab1_frame,
text="Run",
command=self.run1
)
self.button1.grid(row=2, column=1, padx=(0,10), pady=(0,10), sticky='ne')
self.results1 = Results(tab1_frame, 'Results Section 1')
self.results2 = Results(tab1_frame, 'Results Section 2')
self.results3 = Results(tab1_frame, 'Results Section 3')
def run1(self):
#This is the method that will be used to find the results I need
#For the sake of this question, let's pretend I want to put whatever text is in Entry 1, into results section 2 result 2
print (self.product_e.get())
print ('How do I use "widget.config()" to modify the 3 results sections separately???')
self.results1.r2.config(text='1111111111')
self.results2.r2.config(text='xxxxxxxxxx')
self.results3.r2.config(text='----------')
master = Tk()
app = App(master)
master.mainloop()

Categories

Resources