LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-12-2022, 09:55 AM   #1
kleinde66
Member
 
Registered: Nov 2008
Location: Mid Atlantic
Distribution: debian cbpp11 mabox.22.09
Posts: 96
Blog Entries: 9

Rep: Reputation: 2
python3, tkinter, pull down list write to file


good morning all
i am by know means a programmer, with that said i try.
i have this file made from bits and pieces:
Code:
#!/usr/bin/python
# -*- coding: utf-8 -*-

from tkinter import *
from datetime import datetime
import os

root = Tk()
root.geometry( "400x200" )

def show01():
    label001.config( text = clicked001.get() )
    insulin = open("insulin.txt", "a")
    now = datetime.now()
    date = now.strftime(" %d/%m/%Y ")
    time = now.strftime(" %H:%M:%S:%f ")
    insulin.write(f' {time}')
    insulin.write(f' {date}')
    insulin.write(f' {label001 }\n')
    insulin.close()


pop001 = [
    "20 units novolog",
    "21 units novolog",
    "22 units novolog",
    "23 units novolog",
    "24 units novolog",
    "25 units novolog",
    "26 units novolog",
    "27 units novolog",
    "28 units novolog",
    "29 units novolog",
    "51 units basaglar",
    "52 units basaglar",
    "53 units basaglar",
    "54 units basaglar",
    "55 units basaglar",
    "56 units basaglar",
    "57 units basaglar",
    "58 units basaglar",
    "59 units basaglar",
    "60 units basaglar"
]

clicked001 = StringVar()
clicked001.set( "24 units novolog" )

drop001 = OptionMenu( root , clicked001 , *pop001 )
drop001.place(x=20, y=20)

button001 = Button( root , text = "click Me" , command = show01 ).place(x=175, y=20)

label001 = Label( root , text = " " )
label001.place(x=275, y=20)

def show02():
    label002.config( text = clicked002.get() )

pop002 = [
    "3 x eggs",
    "oatmeal",
    "salad",
    "peanuts",
    "porkchops",
    "1 x hamburg",
    "2 x hamburg",
    "shepards pie",
    "3 x pizza",
    "5 x pizza",
    "ice cream",
    "ice cream cake",
    "cake"
]


clicked002 = StringVar()
clicked002.set( "3 x eggs" )

drop002 = OptionMenu( root , clicked002 , *pop002 )
drop002.place(x=20, y=55)

button002 = Button( root , text = "click Me" , command = show02 ).place(x=175, y=55)

label002 = Label( root , text = " " )
label002.place(x=275, y=55)

root.mainloop()
the code is not finished.
it will run, a work in progress.
goal is to record insulin and food intake.
in two text files with time and date stamp.

my first problem is when i try to record the contents of insulin spin box.
line 19 :
Code:
     insulin.write(f' {label001 }\n')
what ever i put in there it isn't giving me what i need:

Code:
  22:46:08:956981   11/09/2022  PY_VAR0
  22:46:30:507360   11/09/2022  PY_VAR0
  23:11:18:516333   11/09/2022  <bound method Variable.set of <tkinter.StringVar object at 0x7f66369ab250>>
  23:12:51:642025   11/09/2022  PY_VAR2
  23:13:23:440598   11/09/2022  PY_VAR3
  23:13:24:232237   11/09/2022  PY_VAR4
  23:14:48:589680   11/09/2022  .!label

i have tried several things, the out put should look like:

Code:
  23:14:48:589680   11/09/2022  (contents of spin box)
def show02 works but it doesn't time and date stamp.
i'm going to have the same thing with time/date with it too.

p.s.
after i have the two files, i'm going to try to use that data in a existing data base .sqlite from my glucose meter.

thanks in advance for any help or guidance.
 
Old 09-12-2022, 10:54 AM   #2
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,597

Rep: Reputation: 2545Reputation: 2545Reputation: 2545Reputation: 2545Reputation: 2545Reputation: 2545Reputation: 2545Reputation: 2545Reputation: 2545Reputation: 2545Reputation: 2545

Your title says "python3" but your script says "/usr/bin/python" which is usually Python 2.

You should check with "/usr/bin/python --version" and if it isn't 3.something then switch the script to either "/usr/bin/python3" or "/usr/bin/env python3"


To get the contents of the dropdown, you should read the contents of the dropdown, i.e. change
Code:
insulin.write(f' {label001 }\n')
to
Code:
insulin.write(f' {clicked001.get()}\n')
There are a number of other things I would do differently in your script, but that's the simplest change to get it doing what you ask.


Since you mention SQLite, I will point out that its primary date/time format is ISO 8601 and it's usually a good idea to use a variation of that for any log files - e.g. "now.strftime('%Y-%m-%d %H:%M:%S.%f')"

 
1 members found this post helpful.
Old 09-12-2022, 11:53 AM   #3
kleinde66
Member
 
Registered: Nov 2008
Location: Mid Atlantic
Distribution: debian cbpp11 mabox.22.09
Posts: 96

Original Poster
Blog Entries: 9

Rep: Reputation: 2
thank you boughtonp
worked great
will my two text files into-grate with my .sqlite file?
 
Old 09-12-2022, 12:13 PM   #4
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,597

Rep: Reputation: 2545Reputation: 2545Reputation: 2545Reputation: 2545Reputation: 2545Reputation: 2545Reputation: 2545Reputation: 2545Reputation: 2545Reputation: 2545Reputation: 2545
Quote:
Originally Posted by kleinde66 View Post
will my two text files into-grate with my .sqlite file?
If you format them as CSV files, you could probably use the Sqlite import command to read their data.

Or you could simply use Python's sqlite3 module to interface directly with a database file instead of the text files.


(Either way, you'll want to ensure you have a safe verified backup of all important data.)

 
Old 09-12-2022, 01:21 PM   #5
kleinde66
Member
 
Registered: Nov 2008
Location: Mid Atlantic
Distribution: debian cbpp11 mabox.22.09
Posts: 96

Original Poster
Blog Entries: 9

Rep: Reputation: 2
thank you boughtonp
i looked at your two suggestions and will have to do a lot of reading, i know nothing of CSV files and as far as the database files
i only have access to them after they are downloaded from the meter. so i will have to add my files to the file created by the meter.
 
Old 09-12-2022, 03:00 PM   #6
kleinde66
Member
 
Registered: Nov 2008
Location: Mid Atlantic
Distribution: debian cbpp11 mabox.22.09
Posts: 96

Original Poster
Blog Entries: 9

Rep: Reputation: 2
boughtonp
i chose to convert the text file to csv file.
then i went in and changes the program to write straight to csv file.
the files seem to be fine, DB Browser for SQLite Version 3.12.2 opens the files.
now i need to learn how to plot.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] how to start python3.6 interpreter just by typing python in terminal not python3.6 bmohanraj91 Linux - Newbie 4 05-10-2017 07:51 AM
Check type of input from user - Python3.4/Tkinter leprechaun3 Programming 1 08-22-2016 12:48 AM
After upgrade python3.4 to python3.5.1 , not able to install packages "request" though pip3 YOGESHAS87 Linux - Software 1 08-03-2016 10:38 PM
Please critique my python3.4/tkinter code (short program) leprechaun3 Programming 3 07-21-2016 12:33 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration