簡體   English   中英

使用 tkinter 在 python 上輸入對話框

[英]Input dialog box on python using tkinter

我正在嘗試創建一個打開 tkinter 窗口的函數,以便用戶可以輸入 2 個點的位置。 類似於 Matlab 的inputdlg函數。

“GUI”有一個按鈕,點擊后關閉“輸入框”並獲得分數。

到目前為止,我能夠創建窗口,但我無法檢索點。 如果我選擇打印值而不是返回值,這將運行。

誰能告訴我代碼有什么問題?

import tkinter as tk

root=tk.Tk() # this will create the window

def get_value(event):

    fpoint=int(entry1.get()) # Gets point 1
    lpoint=int(entry2.get()) # Gets point 2
    points=[fpoint,lpoint]
    root.destroy()           # destroys Gui
    return(points)           # Gets the points

box1=tk.Label(root, text="First point")  # Label of box 1
entry1=tk.Entry(root)                    # Box 1

box2=tk.Label(root, text="Last point")   # Label of box 2
entry2=tk.Entry(root)                    # box 2

Done_button=tk.Button(root, name="done") #Button to terminate the "gui"
Done_button.bind("<Button-1>",get_value) # Run function "get_value" on click    
box1.grid(row=1, sticky="W",padx=4)             # position of label for box1
entry1.grid(row=1,column=1,sticky="E", pady=4)  # position of box 1
box2.grid(row=2, sticky="W",padx=4)             # position of label for box2
entry2.grid(row=2,column=1,sticky="E", pady=4)  # position of box2
Done_button.grid(row=3,column=1)                # position of "button

root.mainloop()

正如deets在評論中指出的那樣,您需要在函數中修改父變量或全局變量的值。

此外,在Done_button.bind("<Button-1>",get_value)使用事件處理程序是不必要的並且也是限制性的,因為它使該函數僅在您用鼠標左鍵單擊它時才起作用。 這意味着例如當按鈕具有焦點時,即使這是一個有效的按鈕按下,按下空格也不會執行任何操作。 相反,您可以使用tk.Button command選項在每次按下按鈕時調用一個函數。

我已經編輯了您的代碼並添加了以下幾行:

    ####### MODIFY A VARIABLE IN PARENT #######
    root.geometry(entry1.get() + "x" + entry2.get())

用於修改 parent 的值,以及:

####### Configuring the button to call a function when pressed  #######
Done_button.configure(command=get_value)

以下是帶有建議更改的整個代碼:

import tkinter as tk

root=tk.Tk() # this will create the window

def get_value():

    fpoint=int(entry1.get()) # Gets point 1
    lpoint=int(entry2.get()) # Gets point 2
    points=[fpoint,lpoint]

    ####### MODIFY A VARIABLE IN PARENT #######
    root.geometry(entry1.get() + "x" + entry2.get())

box1=tk.Label(root, text="First point")  # Label of box 1
entry1=tk.Entry(root)                    # Box 1

box2=tk.Label(root, text="Last point")   # Label of box 2
entry2=tk.Entry(root)                    # box 2

Done_button=tk.Button(root, name="done") #Button to terminate the "gui"

####### Configuring the button to call a function when pressed  #######
Done_button.configure(command=get_value)
#Done_button.bind("<Button-1>",get_value) # Run function "get_value" on click
box1.grid(row=1, sticky="W",padx=4)             # position of label for box1
entry1.grid(row=1,column=1,sticky="E", pady=4)  # position of box 1
box2.grid(row=2, sticky="W",padx=4)             # position of label for box2
entry2.grid(row=2,column=1,sticky="E", pady=4)  # position of box2
Done_button.grid(row=3,column=1)                # position of "button



root.mainloop()

暫無
暫無

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

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