2017-08-05 67 views
-1

我試圖用.get()獲取par02_1。我已經成功地編寫了.txt文件的其餘部分,但我的程序並沒有抓住我的GUI入口線。我猜這與聯想自我和可滾動部分等有關,但我無法追蹤這一個。任何幫助表示讚賞。在tkinter中使用.get()ScrolledFrame

from tkinter import * 
from tkinter import ttk 
import tkinter.messagebox 
import time 

root = Tk() 

# GUI Window 
root.title("Outfile Automation") 


# Gui Future Menu Logic 
def future01(): 
    print("Future Command 01") 


# GUI Main Menu 
menu = Menu(root) 
root.config(menu=menu) 

subMenu = Menu(menu) 
menu.add_cascade(label="File", menu=subMenu) 
subMenu.add_command(label="New Script...", command=future01) 
subMenu.add_separator() 
subMenu.add_command(label="Exit Program", command=future01) 

helpMenu = Menu(menu) 
menu.add_cascade(label="Help", menu=helpMenu, command=future01) 
helpMenu.add_command(label="Instruction Manual", command=future01) 
helpMenu.add_command(label="Software Version: A0.003") 


##Scrollbar 

class VerticalScrolledFrame(Frame): 
    def __init__(self, parent, *args, **kw): 
     Frame.__init__(self, parent, *args, **kw) 

     # create a canvas object and a vertical scrollbar for scrolling it 
     vscrollbar = Scrollbar(self, orient=VERTICAL) 
     vscrollbar.pack(fill=Y, side=RIGHT, expand=FALSE) 
     canvas = Canvas(self, bd=0, highlightthickness=0, 
         yscrollcommand=vscrollbar.set) 
     canvas.pack(side=LEFT, fill=BOTH, expand=TRUE) 
     vscrollbar.config(command=canvas.yview) 

     # reset the view 
     canvas.xview_moveto(0) 
     canvas.yview_moveto(0) 

     # create a frame inside the canvas which will be scrolled with it 
     self.interior = interior = Frame(canvas) 
     interior_id = canvas.create_window(0, 0, window=interior, 
              anchor=NW) 

     # track changes to the canvas and frame width and sync them, 
     # also updating the scrollbar 
     def _configure_interior(event): 
      # update the scrollbars to match the size of the inner frame 
      size = (interior.winfo_reqwidth(), interior.winfo_reqheight()) 
      canvas.config(scrollregion="0 0 %s %s" % size) 
      if interior.winfo_reqwidth() != canvas.winfo_width(): 
       # update the canvas's width to fit the inner frame 
       canvas.config(width=interior.winfo_reqwidth()) 

     interior.bind('<Configure>', _configure_interior) 

     def _configure_canvas(event): 
      if interior.winfo_reqwidth() != canvas.winfo_width(): 
       # update the inner frame's width to fill the canvas 
       canvas.itemconfigure(interior_id, width=canvas.winfo_width()) 

     canvas.bind('<Configure>', _configure_canvas) 


if __name__ == "__main__": 
    class AutoApp(Tk): 
     def __init__(self, *args, **kwargs): 
      root = Tk.__init__(self, *args, **kwargs) 

      self.frame = VerticalScrolledFrame(root) 
      self.frame.pack() 
      self.label = Label(text="") 
      self.label.pack() 

      self.par02_1 = StringVar() 

      title_1 = Label(self.frame.interior, text="Device IP (DHCP)", fg="blue", font="Verdana 10 underline") 
      title_1.pack() 
      label_1 = Label(self.frame.interior, text="Device 01") 
      label_1.pack() 
      entry_1 = Entry(self.frame.interior, textvariable=self.par02_1) 
      entry_1.pack() 

      outFile = open('CSC.txt', 'wt') 
      outFile.write("[Script Header]\nDebugStrings=on\nAbortOnError=on\nConcurrentSectionLimit=230\n" 
          "//23 Devices if necessary""\n\n[Variables]""\n" 
          + (self.par02_1.get()) + "\n\n" 
          "[Device01]\nConnect=tcp |proc01|\nRunAsSingleTransaction=on\nEthernetDHCPHost " 
          + (self.par02_1.get()) + "EthernetCurrentConnectionInfo\n") 

      outFile.close() 

      def muser(): 
       feedback_1 = Label(root, text="Creating Script...").pack() 
       feedback_2 = Label(root, text="Script Complete!").pack() 
       time.sleep(2) 
       return 

      # GUI Buttons 
      mbutton = Button(root, text="Create Script", command=muser, fg="black", bg='green') 
      mbutton.pack() 


    app = AutoApp() 
    app.mainloop() 
+0

您正在嘗試寫出__init__方法中的值 - 不這樣做 - 您的實例甚至在此時未初始化。創建另一種方法(以及調用或運行它的方法),它將在填寫GUI條目後將文件寫出*。 – Gerrat

+0

「_configure_canvas」和「_configure_interior」和「muser」縮進了一個額外的級別,還是粘貼代碼時出現錯誤? –

+0

對不起,很多工作是從其他在線的例子,這是我的第一個程序/ python的經驗,所以我不知道文件中的所有語法。我知道muser命令必須縮進以顯示gui中我想要的位置。 –

回答

0

你好,謝謝你的指導,因爲我學習了正確的語法。我有很長的路要走,但你們都幫助我指出了正確的方向,所以我可以讓我的計劃在線。我將其簡化爲基礎知識,並在我的實際代碼中實現以下體系結構的變體。

from tkinter import * 
from tkinter import ttk 
import tkinter.messagebox 
import time 

root = Tk() 

# GUI Window 
root.title("Outfile Automation") 


# Gui Future Menu Logic 
def future01(): 
    print("Future Command 01") 


# GUI Main Menu 
menu = Menu(root) 
root.config(menu=menu) 

subMenu = Menu(menu) 
menu.add_cascade(label="File", menu=subMenu) 
subMenu.add_command(label="New Script...", command=future01) 
subMenu.add_separator() 
subMenu.add_command(label="Exit Program", command=future01) 

helpMenu = Menu(menu) 
menu.add_cascade(label="Help", menu=helpMenu, command=future01) 
helpMenu.add_command(label="Instruction Manual", command=future01) 
helpMenu.add_command(label="Software Version: A0.003") 

par02_1 = StringVar() 

title_1 = Label(root, text="Device IP (DHCP)", fg="blue", font="Verdana 10 underline") 
title_1.pack() 
label_1 = Label(root, text="Device 01") 
label_1.pack() 
entry_1 = Entry(root, textvariable=par02_1) 
entry_1.pack() 


root.mainloop() 

template = """[Script Header]\nDebugStrings=on\nAbortOnError=on\nConcurrentSectionLimit=230\n//23 Devices if necessary 
       \n\n[Variables]\n{}\n\n[Device01]\nConnect=tcp |proc01|\nRunAsSingleTransaction=on\nEthernetDHCPHost\nEthernetCurrentConnectionInfo\n""" 


outFile = open('CSC.txt', 'wt') 
outFile.write(template.format(par02_1.get()).rstrip()) 

outFile.close() 

root.mainloop()