Home > Enterprise >  Stuck trying to save a file from tkinter entry box into .txt file
Stuck trying to save a file from tkinter entry box into .txt file

Time:01-28

Not really sure what im doing in this stage tbh, found someone asking a similar thing and tried to include it. However im not sure how to integrate it and at the moment they both do their own tkinter windows. One saving a .txt file, the other producing what ive written.

import sys
import tkinter as tk
from tkinter import *
from tkinter.messagebox import showinfo

root = tk.Tk()
root.wm_title('QR Code Generator')



def login():
    frame = Frame(root)
    Label(frame, text = "Welcome to QR Code Generator").grid(row = 0)
    Label(frame, text = "Enter the link you want as a QR Code ").grid(row = 1)
    e1 = Entry(frame)
    e1.grid(row=1, column = 1)
    Button(frame, text = 'Continue', command = save).grid(row = 4, column = 1, sticky = W, pady = 4)
    return frame

def save():
    file_name = entry.get()
    with open(file_name   '.txt', 'w') as file_object:
        file_object.write(file_name)

if __name__ == '__main__':
    top = tk.Tk()
    entry_field_variable = tk.StringVar()
    entry = tk.Entry(top, textvariable=entry_field_variable)
    entry.pack()
    tk.Button(top, text="save", command=save).pack()

login_frame = login()
login_frame.pack(fill="both", expand=True)

root.mainloop()

wanting the "paste link for qr code" section to be saved into a .txt

CodePudding user response:

  • Your filename is not defined anywhere in the code
  • I.e. your filename is empty, this results in a file with no name '.txt'
def save():
    file_name = entry.get()
    with open(file_name   '.txt', 'w') as file_object:
        file_object.write(file_name)
  • Your code opens two windows, it is being created in this line, whenever you define another instance of Tk() it will be a new window, if you need it, I recommend Toplevel
if __name__ == '__main__':
    top = tk.Tk()
  • Try something cleaner and easier to understand
import tkinter as tk
from tkinter import *

root = tk.Tk()
root.wm_title('QR Code Generator')


def save():
    content = e1.get()
    with open('your_file.txt', 'w') as file_object:
        file_object.write(content)


frame = Frame(root)
frame.pack()

Label(frame, text="Welcome to QR Code Generator").pack()
Label(frame, text="Enter the link you want as a QR Code ").pack()

e1 = Entry(frame)
e1.pack()

Button(frame, text='Continue', command=save).pack()

if __name__ == '__main__':
    root.mainloop()

CodePudding user response:

The qrcode is created as a picture. You can write a picture as base64 encoded to a text file, but I recommend to save the picture as an image. I understood your request as "create" and "show" the saved picture into a tkinter window. I saved the picture with a timestamp.

My prototype:

import tkinter as tk
import qrcode
from PIL import ImageTk, Image
import time

root = tk.Tk()
root.title('QR Code Generator')
root.geometry("450x420")
#root.state("zoomed") whole display size
root.config(bg="#2c3e50")

#root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)

def create_QR(link_input):
    print(link_input.get())
    lnk = link_input.get()
    global qr_img
    qr = qrcode.QRCode(version=1, error_correction = qrcode.constants.ERROR_CORRECT_L, box_size=10, border=3)
    qr.add_data(lnk)
    qr.make(fit=True)
    
    time_str = str(int(time.time()))
    img = qr.make_image(fill_color='cyan', back_color='#2c3e50')
    img.save(f'qrcode_{time_str}.png')
    qr_img = f'qrcode_{time_str}.png'
    return qr_img
    
def show_qr():
    global qr_img
    qr_img = ImageTk.PhotoImage(Image.open(qr_img))
 
    qr = tk.Label(frame, image = qr_img)
    qr.grid(row =3, columnspan = 3, padx = 5, pady = 5)
    qr.config(image = qr_img)
    return qr_img

    
l1 = tk.Label(root, text = "Welcome to QR Code Generator", font=("Calibre", 16), bg="#2c3e50", fg="white")
l1.grid(row =0, columnspan = 2, padx = 5, pady = 5)

frame = tk.Frame(root)
frame.grid()
frame.config(bg="#2c3e50")

l2 = tk.Label(frame, text = "Link you want as a QR Code: ", bg="#2c3e50", fg="white")
l2.grid(row =1, column = 1, padx = 5, pady = 5, sticky="w")

link_name = tk.StringVar(frame, value='hhtps://')
e1 = tk.Entry(frame, textvariable = link_name, width=35)
e1.grid(row =1, column = 2, padx = 5, pady = 5)

b_cre = tk.Button(frame, text = 'Create QR Code', command = lambda: create_QR(link_name))
b_cre.grid(row =2, column = 1, padx = 5, pady = 5)

b_sav = tk.Button(frame, text = 'Show QR Code', command = show_qr)
b_sav.grid(row =2, column = 2, padx = 5, pady = 5)

root.mainloop()

Output: enter image description here

  • Related