Пытаюсь поместить то, что я сделал в своем Tkinter GUI, прямо в файл - PullRequest
0 голосов
/ 28 мая 2020
from tkinter import *

root = Tk()'
root.geometry('500x500')
root.title("Auto Assign Widget")

cw=StringVar()
pp=StringVar()
mt=StringVar()
bdt=StringVar()

def printt():   #Print command
    cw1=cw.get()
    pp1=pp.get()
    mt1=mt.get()
    bdt1=bdt.get()
    print("M48")
    print(f"Cu, {cw1}")
    print(f"PP, {pp1}")
    print(f"Mat, {mt1}")
    print(f"Thk, {bdt1}")
    print("%")

def exitt():  #Defining the exit fucntion for the exit button command
    exit()

label_0=Label(root, text="Schmoll Auto Assign Widget", relief='solid')
label_0.place(x=200, y=150)

list1=['Qoz', 'Toz', 'Hoz', '1oz']
droplist=OptionMenu(root, cw, *list1)
cw.set("Copper Weight")
droplist.config(width=15)
droplist.place(x=80, y=240)

list2=['1x106', '1x1027', '1x1080', '1x1067']
droplist=OptionMenu(root, pp, *list2)
pp.set("Pre-Preg Style")
droplist.config(width=15)
droplist.place(x=80, y=280)

list3=['370HR', 'MEG6', 'R6202', 'FR408']
droplist=OptionMenu(root, mt, *list3)
mt.set("Material Type")
droplist.config(width=15)
droplist.place(x=80, y=320)

label_1=Label(root, text='Board Thickness')
label_1.place(x=80, y=360)
entry_1= Entry(root, textvar=bdt)
entry_1.place(x=240, y=360)


but_export = Button(root, text = 'Export', command=printt).place(x=150, y=450)
but_quit = Button(root, text='Quit', command=exitt).place(x=280, y=450)

root.mainloop()

А теперь я хотел бы попробовать создать еще одну кнопку, которая устанавливает путь инструмента к каталогу, чтобы я мог выбрать, в какой файл я хотел бы записать информацию. Есть идеи, где мне начать искать? Я полагаю, что могу сделать это в Tkinter довольно просто

с помощью open ('out.txt', 'w') как f:

print ('Filename:', filename, file = f)

^ Это вроде того, что у меня было в моем, но id хотел бы назначить его команде кнопки, где, возможно, я бы открыл файл, а затем сделал бы свой GUI материал, а затем записал бы его. Спасибо за всю помощь!

1 Ответ

0 голосов
/ 28 мая 2020

Добавьте эти строки в свой код:

1-import filedialog:

from tkinter import filedialog

2-in function printt add:

text="M48\n"+(f"Cu, {cw1}\n")+(f"PP, {pp1}\n")+(f"Mat, {mt1}\n")+(f"Thk, {bdt1}")+"%"
return text

3- Определить новый функция сохранения:

def file_save():
    text = printt()    
    f = filedialog.askopenfilename(initialdir = "/",title = "Select file",filetypes = (("text files","*.txt"),("all files","*.*")))

    if f is None: 
        return
    with open(f, 'r') as original: data = original.read()
    with open(f, 'w') as modified: modified.write(text+"\n" + data)

4- создать кнопку для сохранения:

but_SaveAs = Button(root, text = 'Save as', command=file_save).place(x=50, y=450)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...