如何将按钮属性从输出文件中获取文本?

时间:2016-05-11 12:19:46

标签: python python-3.x button tkinter output

我需要的是制作视图订单按钮以从Customer.txt文件中获取文本并将其设置在我创建的文本字段中。

#make order,cancel,view
from tkinter import *
import tkinter.messagebox

root = Tk()

file = open("Customer.txt", "w")
def textW():
    outFile = open("Customer.txt", "wt")


def CancelOrder():
    outFile=open("Customer.txt", "w")
    outFile.write("")
    tkinter.messagebox.showinfo("Cancel Order", "Your order has been canceled")

def ViewOrder():
    outFile = open('Customer.txt', 'r')
    test = outFile.read()
    #tViewOrder.set(test)
    print (test)
    #test.set(tViewOrder)
    #outFile.close()

def MakeOrder():
    outFile=open("Customer.txt", "w")
    outFile.write("" + tMakeOrder.get())
    tkinter.messagebox.showinfo("Make Order", "Order has been placed. Thank you!")

#Labels
lMakeOrder = Label(root, text="Make an order")
lViewOrder = Label(root, text="View Order")
#TextFields
tMakeOrder = Entry(root)
tViewOrder = Entry(root, state="disabled")

#Buttons
bMakeOrder = Button(root, text="Make order",bg="black",fg="green", command=MakeOrder)
bCancelOrder = Button(root, text="Cancel order",bg="black",fg="green", command=CancelOrder)
bViewOrder = Button(root, text="View orders",bg="black",fg="green", command=ViewOrder)

#Position
lMakeOrder.grid(row=0)
lViewOrder.grid(row=1)

tMakeOrder.grid(row=0, column=2)
tViewOrder.grid(row=1, column=2)

bMakeOrder.grid(row=4)
bViewOrder.grid(row=4, column=2)
bCancelOrder.grid(row=4, column=4)

#Window stuff
root.title("Sky is a shit name service - Customer")
root.geometry("300x300")

root.mainloop()

1 个答案:

答案 0 :(得分:2)

您可以通过调用插入函数将文本放入条目中。

MyEntry.insert(POSITION, TEXT)

哦还有一件事。如果禁用,您不能在条目中插入任何内容。 所以这是你修改过的函数:

def ViewOrder():
    outFile = open('Customer.txt', 'r')
    test = outFile.read()
    tViewOrder['state'] = 'normal'
    tViewOrder.delete(0, 'end')         #Remove everything before
    tViewOrder.insert(0, test)
    tViewOrder['state'] = 'disabled'
    outFile.close()