-1

I am learning to code and just starting with python. I built my first "on my own" app that i plan to use to automate some typing at work when sending in DHCP requests. I am having an issue with calling multiple entries that i've set to variables inside a multiline string. I've tried using StringVar() along with textvariable inside the entry (). Any advice would be appreciated. I believe the issue is with the str1 variable but my lack of experience and understanding has me stumped. The results come back with variables PY_VAR0 which means i have something messed up and the program doesn't know where to look (I think).

from Tkinter import *

win = Tk()

win.minsize(width=300, height=200)
win.maxsize(width=650, height=500)

#Definitions
def dhcp():
    print str1
lab1 = Label(win, text="PC Name: ")
lab2 = Label(win,text="MAC Address: ")
lab3 = Label(win, text="IP Address")
but1 = Button(win, text="DHCP", command=dhcp)
but2 = Button(win, text="NODE")
but3 = Button(win, text="BOTH")
pc_name = StringVar()
entry1 = Entry(win, textvariable=pc_name)
mac_add = StringVar()
entry2 = Entry(win, textvariable=mac_add)
ip_add = StringVar()
entry3 = Entry(win, textvariable=ip_add)


str1= """host %s
                {
                       hardware ethernet %s ;
                       fixed-address %s ;
                       option host-name %s ;
                }"""%(pc_name, mac_add, ip_add, pc_name)

#Layout and Framing

lab1.grid(row=0)
lab2.grid(row=1)
lab3.grid(row=2)
entry1.grid(row=0, column=1)
entry2.grid(row=1, column=1)
entry3.grid(row=2, column=1)
but1.grid(row=3, column=0)
but2.grid(row=4, column=0)
but3.grid(row=5, column=0)

win.mainloop()
  • I do not understand... What are you doing with the multiline string? If you want to **get** the value in the `StringVar` you would need to use `pc_name.get()` etc, but doing that right after initialization isn't very helpful. – Tadhg McDonald-Jensen Jun 17 '16 at 16:13
  • I need to insert the pc_name string from user entered entry1 into dhcp at the first and last %s inside str1. – Travis Sloas Jun 17 '16 at 16:35
  • ok... so get rid of the `%(pc_name, mac_add, ip_add, pc_name)` and just store the string with the format marks in place, then when you need to get the formatted version do `formatted_str = str1%(pc_name.get(), mac_add.get(), ip_add.get(), pc_name.get())` – Tadhg McDonald-Jensen Jun 17 '16 at 16:39
  • Words cannot express how much i appreciate your help and knowledge. I am still learning how things work. It works like a charm. – Travis Sloas Jun 17 '16 at 16:47
  • Is there anyway i can use an output like tkMessageBox but I want to be able to copy the output text. – Travis Sloas Jun 17 '16 at 18:51
  • 1: [How do I copy a string to the clipboard on Windows using Python?](http://stackoverflow.com/questions/579687/how-do-i-copy-a-string-to-the-clipboard-on-windows-using-python) or 2: [python tkinter popup window with selectable text](http://stackoverflow.com/questions/13519192/python-tkinter-popup-window-with-selectable-text) both came up in simple google searches. – Tadhg McDonald-Jensen Jun 17 '16 at 18:54

1 Answers1

0

There are two main issues inside your code:

  • Usage of StringVar
  • Time when str1 is built

As Tadhg McDonald-Jensen mentioned in his commend, Tkinter.StringVar is beeing accessed by its get()-Method. so your string build should look like:


str1= """host %s
         {
             hardware ethernet %s ;
             fixed-address %s ;
             option host-name %s ;
         }"""%(pc_name.get(), mac_add.get(), ip_add.get(), pc_name.get())

This assignment should be done inside your dhcp callback. Why? Because it is then that you want to read the data, when the user pressed the button, not when you create the elements.


#Definitions
def dhcp():
    str1= """host %s
             {
                 hardware ethernet %s ;
                 fixed-address %s ;
                 option host-name %s ;
             }"""%(pc_name.get(), mac_add.get(), ip_add.get(), pc_name.get())
    print str1
Community
  • 1
  • 1
R4PH43L
  • 2,122
  • 3
  • 18
  • 30