簡體   English   中英

使用Tkinter設置變量

[英]Setting variables using Tkinter

對使用Tkinter有所打擊-我要制作的是一個運行系統調用功能的小型GUI。

我希望能夠使用GUI將v1,v2,v3設置為字符串項-它們用於“命令”功能。

def system_call( step_name, cmd ):
    try:
        subprocess.check_call(cmd, shell=True)

    except subprocess.CalledProcessError as scall:
        print "Script failed at %s stage - exit code was %s" % (step_name, scall.returncode)
        exit()


def command(v1, v2, v3):
    # Commandline string
    return v1 + " " + v2 + " " + v3

在下面,您將找到接口設置。

# Create and name the window
        root = Tk()
        root.title("GUI - TEST VERSION")

        # Set the variables needed for function

        v1 = StringVar()
        v2 = StringVar()
        v3 = StringVar()

        # Make text entry box
        w = Label(root, text="V1")
        w.pack()
        text_entry = Entry(root, textvariable = v1.get())
        text_entry.pack()

        w = Label(root, text="V2")
        w.pack()
        text_entry = Entry(root, textvariable = v2.get())
        text_entry.pack()

        w = Label(root, text="V3")
        w.pack()
        text_entry = Entry(root, textvariable = v3.get())
        text_entry.pack()

        # Add a 'Run' button 
        b = Button(root, text="Run fuction", command= system_call(Command call, command(v1, v2,v3)))
        b.pack()

        # Call the GUI
        root.mainloop()

收到一條錯誤消息,指出無法容納字符串對象和實例對象。

您以錯誤的方式使用變量。

在這里,您要使用變量本身,而不是內容:

text_entry = Entry(root, textvariable=v1) # remove .get(), same for the other lines

在這里,您要使用內容,而不是變量:

def command(v1, v2, v3):
    return v1.get() + " " + v2.get() + " " + v3.get() # add .get()

此外,當您在綁定system_call功能按鈕,您呼叫的功能,並將結果結合command 而是使用lambda:

b = Button(root, text="Run fuction", command=lambda: system_call('Command call', command(v1, v2,v3)))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM