-1

I want to create a frame on a certain portion of my application, where the serial input coming from my arduino will be displayed there, along with a scrolling bar, in order to scroll up.

Ideally, i would want the functionality of Autoscroll. Basically, what i want to do is to replace the arduino IDE for receiving serial data, with my own program.

I am sure someone has already done it, so i post here for some pointers.

I have already created the basic tkinter window that will lay the foundation of my application.

This is my code (The functionality is incomplete - just graphics):

import tkinter as tk
import tkinter.ttk as ttk
import serial.tools.list_ports


#to be used on our canvas
HEIGHT = 700
WIDTH = 800

# --- functions ---
def serial_ports():    
    return serial.tools.list_ports.comports()

def on_select(event=None):

    # get selection from event    
    print("event.widget:", event.widget.get())

    # or get selection directly from combobox
    print("comboboxes: ", cb.get())

def single_Sensor():    
    clicked_new = int(clicked)
# --- functions ---



# --- main ---
root = tk.Tk() #here we create our tkinter window
root.title("Sensor Interface")

#we use canvas as a placeholder, to get our initial screen size (we have defined HEIGHT and WIDTH)
canvas = tk.Canvas(root, height=HEIGHT, width=WIDTH)
canvas.pack()

#we use frames to organize all the widgets in the screen

# --- frame 1 ---
frame1 = tk.Frame(root)
frame1.place(relx=0, rely=0.05, relheight=0.03, relwidth=1, anchor='nw') #we use relheight and relwidth to fill whatever the parent is - in this case- root

label0 = tk.Label(frame1, text="Select the COM port that the device is plugged in: ")
label0.config(font=("TkDefaultFont", 8))
label0.place(relx = 0.1, rely=0.3, relwidth=0.3, relheight=0.5)


cb = ttk.Combobox(frame1, values=serial_ports())
cb.place(relx=0.5, rely=0.5, anchor='center')
# assign function to combobox
cb.bind('<<ComboboxSelected>>', on_select)
# --- frame 1 ---



# --- frame 2 ---
frame2 = tk.Frame(root, bd=5) #REMOVED THIS bg='#80c1ff' (i used it to see the borders of the frame)
frame2.place(relx=0, rely=0.1, relheight=0.07, relwidth=1, anchor='nw')

#button
button = tk.Button(frame2, text="Measure all Sensors", bg='#80c1ff', fg='red')  #bg='gray'
button.place(relx=0.2, rely=0.5, anchor='center')

#label
label1 = tk.Label(frame2, text="OR, select a single sensor to measure: ")
label1.config(font=("TkDefaultFont", 9))
label1.place(relx = 0.32, rely=0.3, relwidth=0.3, relheight=0.4)

#dropdown
OPTIONS = [0,1,2,3,4,5,6,7]
clicked = tk.StringVar()
clicked.set(OPTIONS[0]) # default value
drop = tk.OptionMenu(frame2, clicked, *OPTIONS)
drop.place(relx = 0.65, rely=0.25, relwidth=0.08, relheight=0.6)

dropDownButton = tk.Button(frame2, text="Measure this sensor", bg='#80c1ff', fg='red', command=single_Sensor) #bg='gray'
dropDownButton.place(relx = 0.85, rely=0.5, anchor='center')
# --- frame 2 ---



# --- frame 3 ---
frame3 = tk.Frame(root, bd=5) #REMOVED THIS bg='#80c1ff' (i used it to see the borders of the frame)
frame3.place(relx=0, rely=0.2, relheight=0.07, relwidth=1, anchor='nw')

stop_button = tk.Button(frame3, text="STOP measurement(s)", bg='#80c1ff', fg='red')
stop_button.place(relx=0.5, rely=0.5, anchor='center')
# --- frame 3 ---



# --- frame 4 ---
frame4 = tk.Frame(root, bd=5)
frame4.place(relx=0, rely=0.3, relheight=0.09, relwidth=1, anchor='nw')

label2 = tk.Label(frame4, text="Select a sensor to plot data: ")
label2.place(relx = 0.1, rely=0.3, relwidth=0.3, relheight=0.5)

clickedForPlotting = tk.StringVar()
clickedForPlotting.set(OPTIONS[0]) # default value
dropPlot = tk.OptionMenu(frame4, clickedForPlotting, *OPTIONS)
dropPlot.place(relx=0.5, rely=0.5, anchor='center')

dropDownButton = tk.Button(frame4, text="Plot sensor data", bg='#80c1ff', fg='red', command=single_Sensor) #bg='gray'
dropDownButton.place(relx = 0.85, rely=0.5, anchor='center')
# --- frame 4 ---


root.mainloop() #here we run our app
# --- main ---

And this is what i get:

enter image description here

As, you can see, there is a lot of space at teh bottom where the frame with the scrolling bar where be. this is where the arduino serial text will appear.

user1584421
  • 3,499
  • 11
  • 46
  • 86
  • Does this answer your question? [How could I get a Frame with a scrollbar in Tkinter?](https://stackoverflow.com/questions/1873575/how-could-i-get-a-frame-with-a-scrollbar-in-tkinter) – stovfl Apr 21 '20 at 21:57
  • Use [ScrolledText](https://docs.python.org/3/library/tkinter.scrolledtext.html) from `scrolledtext` module instead. – acw1668 Apr 22 '20 at 07:51

1 Answers1

3

I recently built something similar in the control program for my robot. Here are some snippets of what I did:

I created the frame and the scroll bar (self is a class inheriting frame):

self.logFrame = tk.Frame(self, **SharedDiscoBot.frameConfig)
self.scrollbar = tk.Scrollbar(self.logFrame)
self.scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
self.log = tk.Text(self.logFrame, width=60, height=10, padx=5, pady=5, takefocus=0, yscrollcommand=self.scrollbar.set, **SharedDiscoBot.textboxConfig)
self.scrollbar.config(command=self.log.yview)
self.log.pack()    
self.logFrame.pack(side=tk.TOP)

I also have a tk IntVar object to set autoscroll and a checkbox that sets it.

self.autoscroll = tk.IntVar()
self.asCheck = tk.Checkbutton(self.checkFrame, variable=self.autoscroll, text='Auto-Scroll   ', **SharedDiscoBot.checkboxConfig)
self.asCheck.select()

And then my program reads serial data in and either parses it or passes it to this function to be displayed on that text area.

 def redirect(self, aString):
        self.log.insert(tk.END, str(aString))
        if self.autoscroll.get() == 1:
            self.log.see(tk.END)        
        return

If you want to see the rest of that code it's part of this project on my github. https://github.com/delta-G/DiscoBot SPecifically: https://github.com/delta-G/DiscoBot/blob/master/GUI/TermFrame.py

Delta_G
  • 2,927
  • 2
  • 9
  • 15
  • The function that reads serial data continuously. Don;t we need to put that inside an infinite loop? – user1584421 May 09 '20 at 16:46
  • Kind of. This whole thing is running inside a larger loop. The idea is that this gets called every few milliseconds from that main loop. – Delta_G May 10 '20 at 00:42