简体   繁体   English

将Tk文本小部件内容复制到文件中

[英]Copy Tk Text Widget Contents Into A File

I'm trying to copy what is inside of a TK Text widget into a file. 我正在尝试将TK文本小部件内的内容复制到文件中。 What I mean by that is, copy all words and letters currently on the textbox and copy them into the file, so when I go into the file I will see what is in the textbox from the last time the program was run. 我的意思是,复制当前文本框中的所有单词和字母,然后将它们复制到文件中,因此,当我进入文件时,我将看到程序上次运行后文本框中的内容。 Here is my code: (some may have seen from earlier) 这是我的代码:(有些可能早先见过)

import datetime
from time import gmtime, strftime
from tkinter import *
root = Tk()
root.geometry("500x800")
text1 = Label(root, text="Menu", font='Verdana, 15')
text1.pack()
coststr = StringVar()
cost = 0
coststr.set(f'£ {cost}')
menu = ["Burger", "Chips", "Milkshake"]
textln = Label(root, text="\n")
textln.pack()
day = datetime.date.today()



def choiceburger():
    global cost
    global coststr
    cost += 1.99
    cost = round(cost, 2)
    coststr.set(f'£ {cost}')
    txt.insert(END, 'Burger £1.99\n',)


def choicechips():
    global cost
    global coststr
    cost += 1.49
    cost = round(cost, 2)
    coststr.set(f'£ {cost}')
    txt.insert(END, 'Chips £1.49\n',)

def choicemilkshake():
    global cost
    global coststr
    cost += 0.99
    cost = round(cost, 2)
    coststr.set(f'£ {cost}')
    txt.insert(END, 'Milkshake £0.99\n',)



def pay():
    global cost
    global coststr
    txt.insert(END, "Total Charges: ")
    txt.insert(END, coststr.get())
    from tkinter import messagebox
    msgbox = messagebox.askokcancel("", "Are You Sure You Want To Buy These Items?")
    printrec()
    if msgbox == True:
        txt.delete('1.0', END)
        cost = 0
        cost = round(cost, 2)
        coststr.set(f'£ {cost}')
        txtboxmsg()
    else:
        quit()


burgerbutton = Button(root, text="    Burger   £1.99     ", command=choiceburger)
burgerbutton.pack()
chipsbutton = Button(root, text="    Chips   £1.49       ", command=choicechips)
chipsbutton.pack()
milksbutton = Button(root, text="  Milkshake   £0.99 ", command=choicemilkshake)
milksbutton.pack()

textln = Label(root, text="\n")
textln.pack()
textln = Label(root, text="\n")
textln.pack()
textln = Label(root, text="\n")
textln.pack()

txt = Text(root)
txt.pack()

def txtboxmsg():
    txt.insert(END, strftime("%H:%M:%S "))
    txt.insert(END, day)
    txt.insert(END, " Jake's Fast Food\n")


txtboxmsg()


def gettxt():
        inputt=txt.get("1.0","end-1c")
        print(inputt)

def printrec():
    f = open(r"F:/fast food code/receipts.txt", "a")
    f.write("\n{}\n".format(gettxt()))
    f.close()

costlabel = Label(root, textvariable=coststr, font='Verdana, 20')
costlabel.pack()
textln = Label(root, text="\n")
textln.pack()
paybutton = Button(root, text="Pay", command=pay, font='Verdana, 25')
paybutton.pack()
textln = Label(root, text="\n")
textln.pack()

You can see that I have made a program that acts like a shop and prints a receipt once done. 可以看到,我制作了一个程序,其作用类似于商店,并在完成后打印收据。 I want to print that receipt (The Text Box, txt ) into a file, F:/fast food code/receipts.txt . 我想将该收据(文本框, txt )打印到文件F:/fast food code/receipts.txt Whenever I save the receipt into the file, I go inside of the file to have a look and I all I get is 'None' as if nothing was saved into the file, which it clearly was. 每当我将收据保存到文件中时,我都会进入文件内部进行查看,而我得到的只是“无”,好像什么都没有保存到文件中,这显然是这样。

To Make it clearer, my question is how can I copy the contents of my Text widget directly into a file? 为了更加清楚,我的问题是如何将“文本”窗口小部件的内容直接复制到文件中?

You are not returning the inputt from the function. 您不必返回inputt从功能。 Try: 尝试:

import datetime
from time import gmtime, strftime
from tkinter import *
root = Tk()
root.geometry("500x800")
text1 = Label(root, text="Menu", font='Verdana, 15')
text1.pack()
coststr = StringVar()
cost = 0
coststr.set(f'£ {cost}')
menu = ["Burger", "Chips", "Milkshake"]
textln = Label(root, text="\n")
textln.pack()
day = datetime.date.today()



def choiceburger():
    global cost
    global coststr
    cost += 1.99
    cost = round(cost, 2)
    coststr.set(f'£ {cost}')
    txt.insert(END, 'Burger £1.99\n',)


def choicechips():
    global cost
    global coststr
    cost += 1.49
    cost = round(cost, 2)
    coststr.set(f'£ {cost}')
    txt.insert(END, 'Chips £1.49\n',)

def choicemilkshake():
    global cost
    global coststr
    cost += 0.99
    cost = round(cost, 2)
    coststr.set(f'£ {cost}')
    txt.insert(END, 'Milkshake £0.99\n',)



def pay():
    global cost
    global coststr
    txt.insert(END, "Total Charges: ")
    txt.insert(END, coststr.get())
    from tkinter import messagebox
    msgbox = messagebox.askokcancel("", "Are You Sure You Want To Buy These Items?")
    if msgbox == True:
        printrec()
        txt.delete('1.0', END)
        cost = 0
        cost = round(cost, 2)
        coststr.set(f'£ {cost}')
        txtboxmsg()
    else:
        quit()


burgerbutton = Button(root, text="    Burger   £1.99     ", command=choiceburger)
burgerbutton.pack()
chipsbutton = Button(root, text="    Chips   £1.49       ", command=choicechips)
chipsbutton.pack()
milksbutton = Button(root, text="  Milkshake   £0.99 ", command=choicemilkshake)
milksbutton.pack()

textln = Label(root, text="\n")
textln.pack()
textln = Label(root, text="\n")
textln.pack()
textln = Label(root, text="\n")
textln.pack()

txt = Text(root)
txt.pack()

def txtboxmsg():
    txt.insert(END, strftime("%H:%M:%S "))
    txt.insert(END, day)
    txt.insert(END, " Jake's Fast Food\n")


txtboxmsg()

def gettxt():
    inputt=txt.get("1.0",END)
    print(inputt)
    return inputt

def printrec():
    f = open(r"receipts.txt", "a")
    f.write("\n{}\n".format(gettxt()))
    f.close()

costlabel = Label(root, textvariable=coststr, font='Verdana, 20')
costlabel.pack()
textln = Label(root, text="\n")
textln.pack()
paybutton = Button(root, text="Pay", command=pay, font='Verdana, 25')
paybutton.pack()
textln = Label(root, text="\n")
textln.pack()

Your issue is in the def gettxt(): function. 您的问题在def gettxt():函数中。

Change this: 更改此:

def gettxt():
        inputt=txt.get("1.0","end-1c")
        print(inputt)

To this: 对此:

def gettxt():
    global txt
    inputt=txt.get("1.0","end-1c")
    print(inputt)
    return inputt

You could simply bypass this function by doing the work in printrec() . 您可以通过在printrec()printrec()工作来绕过此函数。

Try this instead: 尝试以下方法:

def printrec():
    global txt
    f = open(r"./receipts.txt", "a")
    f.write("\n{}\n".format(txt.get("1.0","end-1c")))
    f.close()

Your code has some other issues as well. 您的代码也有其他问题。 Make sure you are using proper indention. 确保使用正确的缩进。 In your gettxt() you have indented 8 spaces instead of 4. You also do not need to have spacers in your labels. gettxt()您缩进了8个空格而不是4个空格。您也不需要在标签中使用空格。 You can use padding to get what you want. 您可以使用填充来获取所需的内容。

For reference I have updated your code to a class and cleaned it up a bit. 作为参考,我已将您的代码更新为一个类,并对其进行了一些整理。 This will prevent the need for global's and provide you some reference to what needs and doesn't need a variable name as well as how you can space things out with padding instead of adding more labels. 这将避免对全局变量的需要,并为您提供一些有关需要和不需要变量名的参考,以及如何使用填充物来分隔内容而不是添加更多标签。

import datetime
from time import strftime
from tkinter import messagebox
# import tkinter as tk helps to prevent an accidental overriding of variable/function names in the namespace.
import tkinter as tk

# Using a class we can get rid of global and manage things much easier using class attributes
class App(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.geometry("500x800")
        # This weight helps the widgets in column 0 resize with the window.
        self.columnconfigure(0, weight=1)

        self.coststr = tk.StringVar()
        self.cost = 0
        self.coststr.set('£ {}'.format(self.cost))
        self.menu = ["Burger", "Chips", "Milkshake"]
        self.day = datetime.date.today()
        #this label is using `pady` to set the padding below the widget so you do not need spacer labels
        tk.Label(self, text="Menu", font='Verdana, 15').grid(row=0, column=0, pady=(0, 20))        
        self.btn_frame = tk.Frame(self)
        self.btn_frame.grid(row=1, column=0)
        # These buttons are placed in a frame so they can expand to fit the frame and be all the same size.
        tk.Button(self.btn_frame, text="Burger: £1.99", command=self.choiceburger).grid(row=0, column=0, sticky="ew")
        tk.Button(self.btn_frame, text="Chips: £1.49", command=self.choicechips).grid(row=1, column=0, sticky="ew")
        tk.Button(self.btn_frame, text="Milkshake: £0.99", command=self.choicemilkshake).grid(row=2, column=0, pady=(0, 80), sticky="ew")

        self.txt = tk.Text(self)
        self.txt.grid(row=2, column=0)

        tk.Label(self, textvariable=self.coststr, font='Verdana, 20').grid(row=3, column=0, pady=(0, 40))
        tk.Button(self, text="Pay", command=self.pay, font='Verdana, 25').grid(row=4, column=0)

        self.txtboxmsg()

    def choiceburger(self):
        self.cost += 1.99
        self.cost = round(self.cost, 2)
        self.coststr.set('£ {}'.format(self.cost))
        self.txt.insert("end", 'Burger £1.99\n',)

    def choicechips(self):
        self.cost += 1.49
        self.cost = round(self.cost, 2)
        self.coststr.set('£ {}'.format(self.cost))
        self.txt.insert("end", 'Chips £1.49\n',)

    def choicemilkshake(self):
        self.cost += 0.99
        self.cost = round(self.cost, 2)
        self.coststr.set('£ {}'.format(self.cost))
        self.txt.insert("end", 'Milkshake £0.99\n',)

    def pay(self):
        self.txt.insert("end", "Total Charges: ")
        self.txt.insert("end", self.coststr.get())
        msgbox = messagebox.askokcancel("", "Are You Sure You Want To Buy These Items?")
        self.printrec()

        if msgbox == True:
            self.txt.delete('1.0', "end")
            self.cost = 0
            self.cost = round(self.cost, 2)
            self.coststr.set('£ {}'.format(self.cost))
            self.txtboxmsg()
        else:
            self.destroy()

    def txtboxmsg(self):
        self.txt.insert("end", strftime("%H:%M:%S "))
        self.txt.insert("end", self.day)
        self.txt.insert("end", " Jake's Fast Food\n")

    def printrec(self):
        # this with open statement will also close once the write is complete.
        with open(r"./receipts.txt", "a") as f:
            # this write statement gets the data directly from txt so you do not need the gettxt() function.
            f.write("\n{}\n".format(self.txt.get("1.0","end-1c")))

app = App()
app.mainloop()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM