How to save data entered in tkinter

Asked 1 years ago, Updated 1 years ago, 431 views

I would like to use tkinter in python to save the data I entered or referenced.
If you look it up online, there is a way to save it using sql, but is there any other way?

What you want to achieve
Click on the image in the script below.

画像 Image acquisition>Save
②Select the image you saved in the folder
③Click Image

If you close the tkinter and reopen it, the data in the selected folder will disappear.
That's why
Is there any new function or other way to save the file on tkinter?

フォ Select the image saved in the folder
Can I see the selected folder after re-opening tkinter?
I'd like to save the folder I finally selected.

May I have your advice?

I would appreciate it if someone could teach me.
Thank you for your cooperation.

Code

import tkinter ask
from PIL import Image, ImageTk
import pyautogui
import time
import subprocess
importos
from tkinter import filedialog
import re

# Start SnippingTool, run when button is clicked
image(event):
    
 p=subprocess.Popen([r"C:\Windows\System32\SnippingTool.exe")

selected_file=""
# Browse files, run when button is clicked
def file_select():
  global selected_file
  idir='C:\\python_test'#Initial folder
  filetype=[("All", "*"), ("Text", "*.txt"), ("Music", "*.mp3")]# Select Extension
  file_path=tk.filedialog.askopenfilename (filetypes=filetype, initialdir=idir)
  selected_file=file_path
  input_box.insert(tk.END, file_path)#View Results
  print(selected_file)

# Click image, run when button is clicked
def click_image():
 global selected_file

 # Replace selected_file from the file reference function to get the coordinates of this PC icon
 position=pyautogui.locateOnScreen(selected_file, confidence=0.9)
 # position = pyautogui.locateOnScreen("C://Users///image//excel.PNG", confidence=0.9)
 # Click the maxwindowPC icon
 pyautogui.doubleClick(position)
 
# screen creation
window=tk.Tk()
window.geometry("300x300")
window.title ("Show Button")

# Image Acquisition Button Creation
btn1=tk.Button (window, text="(1) Image Acquisition")
 
# button display
btn1.place(x=15, y=15, width=150, height=40)

# Bind function to button
btn1.bind("<Button-1>", image)

# Create input field
input_box=tk.Entry(width=40)
input_box.place(x=10,y=100)

# Create Result Labels
input_label=tk.Label(text="2 Select image file")
input_label.place(x=10,y=70)

# Create reference button
button=tk.Button(text="reference", command=file_select)
button.place(x=10,y=130)

# Create Image Click Button
button=tk.Button(text="3 Click Image", command=click_image)
button.place (x=15, y=175, width=150, height=40)
 
# Display (resident)
window.mainloop()

python python3 tkinter

2022-10-13 01:01

1 Answers

If you only need one piece of data, it's easy to read it at startup and update the files every time the data is updated.

If you extract and describe only the relevant parts, it will be as follows.Skip the front and back.

###Retrieve data from the file where the selected file path is stored (change the name and folder appropriately)
savedata_file=os.path.dirname(os.path.abspath(__file__))+'\\savedata.txt'
try:
    with open(savedata_file, 'r', encoding='utf8') asf:
        selected_file=f.read()
except:
    selected_file="C:\\python_test\\*.*"#If the file is missing, the initial folder + dummy filename

# Browse files, run when button is clicked
def file_select():
    global selected_file
    idir=os.path.dirname(os.path.abspath(selected_file))### Extract destination folder
    filetype=[("All", "*"), ("Text", "*.txt"), ("Music", "*.mp3")]# Select Extension
    file_path=tk.filedialog.askopenfilename (filetypes=filetype, initialdir=idir)
    selected_file=file_path
    input_box.insert(tk.END, file_path)#View Results
    print(selected_file)
    #### Save to file if selection path is valid
    ifos.path.exists(selected_file):
        with open(savedata_file, 'w', encoding='utf8') asf:
            f.write(selected_file)

Here is no example, but other methods are also available:

If you want to add a little more data and tie it up, you can use this package.
configparser --- configuration file parser
Configuration file management module in the python program ~how to use and consider configparser~
How to update a value python.ini file with configparser

There are other things like settings and json.
Python Configuration File Management Summary (by settings/ini/json format) [Copype Sample Code Included]
[Python Basics] Consider JSON format files as program configuration files
How are you doing with your own tools?Python Configuration File Championship


2022-10-13 01:01

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.