2017-10-23 1 views
1

私はPythonでGUIを持っていますが、他にもドロップダウンメニューから項目を選択しましょう(私はtkinterのコンボボックス機能を使用しました)。ドロップダウンリストのアイテムがPythonで選択されている場合にユーザに入力を促します

「カスタム」という項目を選択すると、そのカスタム番号の入力を求める入力ボックスが表示されるようにしたいと考えています。ボックスを表示するためにボタンを使用する必要はありませんが、何らかの理由でカスタムが選択されるとすぐに入力ボックスが表示されます。 私がここで見つけたaskinteger()メソッドを使ってみましたが(http://effbot.org/tkinterbook/tkinter-entry-dialogs.htm)、私が思いついた組み合わせはありませんでした。 (私はPythonで非常に新しく、まだ分かりやすい間違いを覚えています)。ここで

は、GUIのための私のコードです:

from tkinter import * 
from tkinter.ttk import * 
from tkinter import filedialog 
from tkinter import StringVar 
from tkinter import messagebox 
from tkinter import simpledialog 


class MyGUI: 
    def __init__(self, master): 

     self.master = master 
     master.title("Intent/Interpretation Check") 

     self.runlabel = Label(master, text="RunID :") 
     self.runlabel.grid(row=0, column=0) 

     self.runentry = Entry(master) 
     self.runentry.grid(row=1, column=0, padx=25) 

     self.checklabel = Label(master, text="Check type :") 
     self.checklabel.grid(row=0, column=1) 

     self.typeselect = Combobox(master) 
     self.typeselect['values']=("Intent Score", "Interpretation Score")  
     self.typeselect.grid(row=1, column=1, padx=25) 

     self.limitlabel = Label(master, text="Fails if score is below :") 
     self.limitlabel.grid(row=0, column=2, padx=25) 

     self.limitselect = Combobox(master) 
     self.limitselect['values']=(1000, 5000, "Custom")  
     self.limitselect.grid(row=1, column=2, padx=25) 
     if self.limitselect.get() != "Custom": 
      self.limit = self.limitselect.get() 
      pass 
     else: 
      self.askinteger("Custom limit", "Please enter a number from 1 to 10000", minvalue=1, maxvalue=10000) 

     self.submitbutton = Button(master, text="Submit", command=self.checkstatus) 
     self.submitbutton.grid(row=1, column=3, padx=25, pady=5) 

root = Tk() 
root.geometry("+600+300") 
my_gui = MyGUI(root) 
root.mainloop() 

は事前にありがとうございました!

答えて

1

新しい入力を表示する必要があることを示すBoolが必要です。 また、値が "Custom"と等しいかどうかをコンボボックスに常にポーリングする必要があります。これは約3分で私が思いついたものです。

私はGUIをきれいに見せようとはしませんでしたが、機能的な例です。

from tkinter import * 
from tkinter.ttk import * 

class Gui: 

    def __init__(self): 
     self.root = Tk() 

     # Set up the Combobox 
     self.selections = Combobox(self.root) 
     self.selections['values'] = ['Apples', 'Oranges', 'Blueberries', 'Bananas', 'Custom'] 
     self.selections.pack() 

     # The Entry to be shown if "Custom" is selected 
     self.custom_field = Entry(self.root) 
     self.show_custom_field = False 

     # Check the selection in 100 ms 
     self.root.after(100, self.check_for_selection) 

    def check_for_selection(self): 
     '''Checks if the value of the Combobox equals "Custom".''' 


     # Get the value of the Combobox 
     value = self.selections.get() 

     # If the value is equal to "Custom" and show_field is set to False 
     if value == 'Custom' and not self.show_custom_field: 

      # Set show_field to True and pack() the custom entry field 
      self.show_custom_field = True 
      self.custom_field.pack() 


     # If the value DOESNT equal "Custom" 
     elif value != 'Custom': 

      # Set show_field to False 
      self.show_custom_field = False 

      # Destroy the custom input 
      self.custom_field.destroy() 

      # Set up a new Entry object to pack() if we need it later. 
      # Without this line, tkinter was raising an error for me. 
      # This fixed it, but I don't promise that this is the 
      # most efficient method to do this. 
      self.custom_field = Entry(self.root) 

     # If the value IS "Custom" and we're showing the custom_feild 
     elif value == 'Custom' and self.show_custom_field: 
      pass 


     # Call this method again to keep checking the selection box 
     self.root.after(100, self.check_for_selection) 


app = Gui() 
app.root.mainloop() 

希望します。

EDIT:

新しいウィンドウを開く代わりに、コンボボックスと同じウィンドウ内に梱包、これで機能check_for_selectionを置き換えるために:あなたは何が起こっているかについて質問がある場合は

def check_for_selection(self): 
    value = self.selections.get() 

    # If the value is equal to "Custom" and show_field is set to False 
    if value == 'Custom' and not self.show_custom_field: 

     # Set show_field to True and pack() the custom entry field 
     self.show_custom_field = True 

     # Create a new window how we did when we made self.root 
     self.new_window = Tk() 

     # Create the Entry that will go in the window. The previous Entry widget from line 16, can be removed 
     self.custom_field = Entry(self.new_window) 
     self.custom_field.pack() 

     # Run the new window like we did the original 
     self.new_window.mainloop() 


    # If the value DOESNT equal "Custom" 
    elif value != 'Custom': 

     # Destroy the new window that was created if it exists 
     if self.show_custom_field: 
      self.new_window.destroy() 

     # Set show_field to False 
     self.show_custom_field = False 

    # If the value IS "Custom" and we're showing the custom_feild 
    elif value == 'Custom' and self.show_custom_field: 
     print('yes') 


    # Call this method again to keep checking the selection box 
    self.root.after(100, self.check_for_selection) 
+0

、ただ質問する – Jebby

+0

ちょっとこれは本当に役立つおかげでトンです!あなたの注釈のおかげでそれをすべて得ましたが、親ウィジェットに入力ボックスを置くのではなく、それが別のウィンドウで表示されるのではないかと思いますか?どういうわけか、新しいウィンドウで、選択時にユーザにプロンプ​​トを表示しますか? –

+0

これは間違いなく実行可能です。私はここに実例を追加します。 – Jebby

関連する問題