Want to display two string value in two different tkinter label - python

I make code for displaying two serial read string in two different label using tkinter GUI..
My code is running and window also popped up but values are not displaying..
Help me to solve this issue..
Here is my code
import serial
import tkinter
from tkinter import *
import time
class SerialViewer(Tk):
def _init_(self):
self.win = Tk()
self.ser=serial.Serial('/dev/ttyS0',9600)
def makewindow (self):
frame1 =Frame(self.win)
frame1.pack()
self.v=StringVar()
self.v.set=('default')
label=Label(frame1,textvariable=self.v,relief=RAISED)
label.pack(side=LEFT)
frame2 = Frame(self.win)
frame2.pack()
self.d=StringVar()
self.d.set=('default')
label=Label(frame2,textvariable=self.d,relief=RAISED)
label.pack(side=RIGHT)
def update(self):
print(self.ser.write("*01T%"))
data=self.ser.readline(self.ser.inWaiting())
self.v.set(data)
time.sleep(2)
print(self.ser.write('*00T%'))
data1=self.ser.readline(self.ser.inWaiting())
self.d.set(data1)
self.win.after(100,self.update)
def run (self):
self.makewindow()
self.update()
self.win.mainloop()
app=SerialViewer()
app.mainloop()

As said in comment that the method _init_ needs to be changed to __init__ and the recursion error is coming because the __init__ method of parent class wasn't called. Please add that call like below:
class SerialViewer(Tk):
def _init_(self):
super(SerialViewer, self).__init__() # Call the initializer method of parent class
self.win = Tk()
self.ser=serial.Serial('/dev/ttyS0',9600)
Hope this helps!!

Related

Creating a new window on pressing a button in an existing window in Tkinter

I am having issues with a Tkinter GUI. I need to create a large application. I need to use classes for that to manage all modules together. For a unit check and getting help here, I have tried to provide a sample which is close to exact problem ( with a small example) here:
I am creating a 1st window with a Button labelled as "Test". What I want is that when I click the button "Test", a new second window will pop up with a text "Enter Value" and entry space, where I can enter the value. I have provided the code below. What is happening is that, I am able to get the new window, but the text "Enter Value" and entry Space is generated in the first window instead of the second and the second window remains blank. I am not understanding where I am making the wrong logic call. Help will be very much appreciated.
I know we do not need classes for GUI applications, however to manage my large application ( not shown here), I need to have classes and I will very much appreciate, if some Tkinter Guru can help me with the bug in my code.
gui view File (gui_view.py)
import tkinter as tk
from tkinter import Tk
class MyMainGUI(tk.Frame):
def __init__(self, controller):
tk.Frame.__init__(self)
self.pack()
self.controller = controller
self.Button1=tk.Button(self)
self.Button1["text"]= "Test"
self.Button1["command"]=self.controller.buttonPressed1
self.Button1.grid(row=2,column=0,rowspan=2)
class MySecondGUI(tk.Frame):
def __init__(self, controller):
tk.Frame.__init__(self)
self.pack()
self.controller = controller
self.outputLabel2 = tk.Label(self)
self.outputLabel2["text"] = ("Enter Value")
self.outputLabel2.grid(row=1,rowspan=2)
#Entry Space
self.entrySpace2 = tk.Entry(self)
self.entrySpace2.grid(row=2,column=0,rowspan=2)
### gui Controller File (gui_controller.py)
import tkinter as tk
import gui_view # the VIEW file
class MainControl:
def __init__(self):
self.root = tk.Tk()
self.root.geometry('550x200')
self.view = gui_view.MyMainGUI(self)
self.view.mainloop()
def newWindow(self):
self.newWin = tk.Toplevel(self.root)
self.newWin.geometry('300x400')
self.newDisplay = tk.Label(self.newWin, text='Test Mode')
self.viewNew = gui_view.MySecondGUI(self.newWin)
self.viewNew.mainloop()
self.newDisplay.pack()
def buttonPressed1(self):
self.newWindow()
if __name__ == "__main__":
c = MainControl()
#
ADDING A MODIFIED CODE WITH NEW PROBLEM.
I have now been able to generate a code which pops up a new window with entries, when I click the button "Test" in the first Window. However, I am having problems creating buttons in the scond window. The way I have it now, it pops an error to me saying "'MySecondGUI' object has no attribute 'buttonPressed2"
Help will be very much appreciated.
I have pasted my updated code below:
GUI_VIEW FILE ( gui_view.py)
import tkinter as tk
from tkinter import Tk
class MyMainGUI(tk.Frame):
def __init__(self, controller):
tk.Frame.__init__(self)
self.pack()
self.controller = controller
self.Button1=tk.Button(self)
self.Button1["text"]= "Test"
self.Button1["command"]=self.controller.buttonPressed1
self.Button1.grid(row=2,column=0,rowspan=2)
class MySecondGUI(tk.Toplevel):
def __init__(self):
tk.Toplevel.__init__(self)
self.outputLabel2 = tk.Label(self)
self.outputLabel2["text"] = ("Enter Value")
self.outputLabel2.grid(row=5,rowspan=2)
self.entrySpace2 = tk.Entry(self)
self.entrySpace2.grid(row=8,column=0,rowspan=2)
self.Button2=tk.Button(self)
self.Button2["text"]= "Try Me"
self.Button2["command"] = self.buttonPressed2
self.Button2.grid(row=14,column=0,rowspan=2)enter code here
GUI MAIN CONTROLLER FILE
import tkinter as tk
import gui_view # the VIEW file
class MainControl:
def __init__(self):
self.root = tk.Tk()
self.root.geometry('550x200')
self.view = gui_view_temp.MyMainGUI(self)
self.view.mainloop()
def newWindow(self):
self.viewNew = gui_view.MySecondGUI()
self.viewNew.geometry('300x400')
self.newDisplay = tk.Label(self.newWin, text='Test Mode')
self.viewNew.mainloop()
self.newDisplay.pack()
def buttonPressed1(self):
self.newWindow()
def buttonPressed2(self):
pass
if name == "main":
c = MainControl()
in MySecondGUI(tk.Frame):
def __init__(self, controller):
#Attach your frame to "secondGUI" (which is your second window)
tk.Frame.__init__(self, controller)
in class MainControl:
#I assume you're passing your parent window/frame as "controller"?
self.viewNew = MySecondGUI(self.newWin)
according to Python Tkinter Docs
https://docs.python.org/3.5/library/tkinter.html#mapping-basic-tk-into-tkinter
You should specify your parent window if you're not attach your widget to main window.
#Main window
root = tk.Tk()
#Second Window
newWin = tk.Toplevel(root)
#Attach to main window
tk.Label(text="This label attached to root").pack()
tk.Button(text="This button attached to root").pack()
#Second Window
tk.Label(newWin, text="This label attached to second window").pack()
tk.Button(newWin, text="This button attached to second window").pack()
also,
self.viewNew.mainloop()
#This will fail because you have to set everything up before mainloop()
#_tkinter.TclError: can't invoke "pack" command: application has been destroyed
self.newDisplay.pack()
Edit for update
You should put your
def buttonPressed2(self):
in class MySecondGUI, not in class MainControl.
class MySecondGUI(tk.Toplevel):
def __init__(self):
tk.Toplevel.__init__(self)
self.outputLabel2 = tk.Label(self)
self.outputLabel2["text"] = ("Enter Value")
self.outputLabel2.grid(row=5,rowspan=2)
self.entrySpace2 = tk.Entry(self)
self.entrySpace2.grid(row=8,column=0,rowspan=2)
self.Button2=tk.Button(self)
self.Button2["text"]= "Try Me"
#self means "MySecondGUI" not "MainControl" here
self.Button2["command"] = self.buttonPressed2
self.Button2.grid(row=14,column=0,rowspan=2)
def buttonPressed2(self):
pass

Modifying a Window Class in Tkinter (Python 3)

I've been trying to get to grips with OOP and tkinter in python 3. I'd really like to have sub-windows pop up during use, either for data, output, etc. However, I cannot figure out how to assign the title in my Windows class, depending on what sort of window is being made. Please find a simplified example of what I have done so far.
from tkinter import *
from tkinter import messagebox
class Window(Frame):
def __init__(self, master = None):
Frame.__init__(self, master)
self.master = master
self.init_window()
def init_window(self):
self.master.title("ProgramName")
self.pack(fill = BOTH, expand = 1)
menu = Menu(self.master)
self.master.config(menu=menu)
prog_help = Menu(menu)
prog_help.add_command(label='Help', command=self.help_popup)
prog_help.add_command(label='About', command=self.version_popup)
menu.add_cascade(label='Help', menu=prog_help)
#Method 1: Using message.box
def version_popup(self):
messagebox.showinfo("About program", "Version 0.1")
return
#Method 2: Using another window
def help_popup(self):
helpwindow()
return
def helpwindow():
hwindow = Toplevel()
hwindow.geometry("100x100")
root = Tk()
root.geometry("400x300")
app = Window(root)
root.mainloop()
I think that I should be doing something in the definition of helpwindow() which alters a variable in self.master.title(x), but I cannot figure out what.
Many thanks for your time.
In your case, the help window isn't a subclass of anything. All you need to do is call the title method of the Toplevel
def helpwindow():
hwindow = Toplevel()
hwindow.title("I am the help window")
...

tkinter variable does not change when changing Checkbutton state

I have a Window wich opens another window askig for the settings. But the BooleanVar I use to get the Checkbutton's state doesn't change. It does however when I call the settingswindow strait from the code without the other window.
This is the minimal code to get the
from tkinter import *
class MainWindow():
def __init__(self, master):
self.root = master
SettingsWindow()
self.root.mainloop()
class SettingsWindow():
def __init__(self):
rootSettings = Tk()
self.rebuild = BooleanVar()
chkRebuild = Checkbutton(rootSettings, text="rebuild", variable=self.rebuild, command=self.testFunc)
chkRebuild.pack()
rootSettings.mainloop()
def testFunc(self):
print(self.rebuild.get())
root = Tk()
mainWindow = MainWindow(root)
The output is always 0 when clicking on the checkbutton, evenso the output of BooleanVar is True or False.
What is the difference in calling SettingsWindow() from insite a class or outside? At least I think that is the reason it is not working.
I am using Pyhton3 in case there is a difference.
You can't have two instances of Tk. For your second window you need to create a Toplevel. You also should never call mainloop more than once in your entire program.
This is a sample example for how to solve the checkbutton's variable not changing issue.
Key: for the second window, should use Toplevel, not use Tk.
from tkinter import *
class MainWindow:
def __init__(self):
self.master = Tk()
self.fun = dict()
def set_ui(self):
Button(self.master, text='SecondWindow', command=self.fun).pack(side=LEFT)
self.master.mainloop()
class SecondWindow:
def __init__(self):
self.root = Toplevel()
self.var = BooleanVar()
self.set_ui()
def printf(self):
print(self.var.get())
def set_ui(self):
Checkbutton(self.root, text='press', variable=self.var, command=self.printf).pack(side=LEFT)
def call_second_window():
second_window = SecondWindow()
main_window = MainWindow()
main_window.fun = call_second_window
main_window.set_ui()

How to create new Tkinter window after mainloop()?

I want to dynamically create Tkinter windows on my screen. I understand that I should only have one mainloop(). I use the threading module to make mainloop execute in a seperate thread, so it will not block the script.
How do I create more Tkinter windows after I executed mainloop?
Please take a look at my code:
from Tkinter import *
import threading
import time
class box:
def __init__(self, pos):
self.master = Tk()
self.master.geometry(pos)
self.canvas = Canvas(self.master, width=50, height=50, highlightthickness=0 )
self.canvas.pack()
self.rect = self.canvas.create_rectangle(0, 0, 50, 50, fill="red", outline="red")
self.text = self.canvas.create_text(25, 24, text="99",fill="white", font=("calibri", 24, "bold"))
def changeFill(self, color):
self.canvas.itemconfig(self.rect, fill=color, outline=color) # change color
class box_manager(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.boxes = {}
self.add_box(1, "50x50+300+300")
self.add_box(2, "50x50+100+100")
def add_box(self, num, pos):
self.boxes[num] = box(pos)
def run(self):
mainloop()
tk = box_manager()
tk.start()
# How do I dynamically add new tkinter windows? the line below makes python.exe crash.
tk.add_box(3, "50x50+200+200")
Update after Joel's comment, still doesn't work:
from Tkinter import *
import threading
import time
class MyCustomWindow(Toplevel):
def __init__(self):
Toplevel.__init__(self)
#setup goes here
self.geometry("50x50+100+100")
self.canvas = Canvas(self, width=50, height=50, highlightthickness=0 )
self.canvas.pack()
class App(Tk):
def CreateFirst(self):
self.anotherWindow = MyCustomWindow()
def CreateSecond(self):
self.secondWindow = MyCustomWindow()
class SecondWindow(threading.Thread):
#after 2 seconds create a second window, python.exe crashes
def run(self):
time.sleep(2)
tk.CreateSecond()
SecondWindow().start()
tk = App()
tk.CreateFirst()
mainloop()
How do I create more Tkinter windows after I executed mainloop?
You don't. That's not how Tkinter is designed to work. You should always call mainloop exactly once, and from the main thread.
Additional (non-root) windows are simply Toplevel widgets. You would simply subclass Toplevel, and call it from within your main class:
class MyCustomWindow(tkinter.Toplevel):
def __init__(self):
tkinter.Toplevel.__init__(self)
#setup goes here
class App(tkinter.Tk):
def someCallback(self):
self.anotherWindow = MyCustomWindow()
EDIT
You don't have to subclass Toplevel of course, you can use it directly.

updating listbox from another class in tkinter

What i'm trying to do is in the onopen function in Controller class i'm trying to run the update_listbox function in the View class which will update the listbox. This is giving me the error update_listbox() must be called with View instance as first argument. I don't fully understand what i'm doing so it would be very helpful if someone could explain to me what i'm doing wrong here and how to do it correctly.
cheers tchadwik
from Tkinter import *
import tkMessageBox
import tkFileDialog
from tkFileDialog import askopenfilename
from tkMessageBox import askokcancel
class Controller(object):
def __init__(self, master=None):
self._master = master
#filemenubar
self.menu()
#listbox
self.listboxFrame = View(master)
self.listboxFrame.pack(fill=BOTH, expand=YES)
#entry widget
self.EntryFrame = Other(master)
self.EntryFrame.pack(fill = X)
def menu(self):
menubar = Menu(self._master)
self._master.config(menu=menubar)
fileMenubar = Menu(menubar)
fileMenubar.add_command(label="Open Products File", command=self.onopen)
fileMenubar.add_command(label="Save Products File", command=self.onsave)
fileMenubar.add_command(label="exit", command=self.onExit)
menubar.add_cascade(label="File", menu=fileMenubar)
def onopen(self):
fname = askopenfilename()
products.load_items(fname)
View.update_listbox() #
#this gives me error stating that it needs View instance as first argument
#and adding self here only gives it the controller instance
class View(Frame):
def __init__(self, master):
Frame.__init__(self, master)
self.list = Listbox(self, selectmode=EXTENDED)
self.list.pack(fill=BOTH, expand=YES)
self.current = None
self.update_listbox()
def update_listbox(self):
temp=products.get_keys()
for i in temp:
self.list.insert(END, str(products.get_item(i)))
You need to instantiate a View object. For example:
def onopen(self):
fname = askopenfilename()
products.load_items(fname)
myview = View(self._master) # Instantiate the object
myview.update_listbox() # Now call update_listbox()
This is because the memory for the member variables (for example: self.list) is not allocated until the object is instantiated. Or another way to put it is that self.list is not created until View.__init__() is called, which happens when you create a View object from the View class definition.

Categories