Python tkinter:当选择某个下拉值时,如何使按钮执行其他操作?

Python tkinter:当选择某个下拉值时,如何使按钮执行其他操作?,python,list,user-interface,if-statement,tkinter,Python,List,User Interface,If Statement,Tkinter,我正在尝试制作一个下拉菜单,当你按下按钮时,它会将输入的文本转换为二进制,或者将输入的二进制转换为文本,具体取决于你在下拉菜单中选择的内容。我制作了一个简单的下拉菜单,并将列表中的第一个值作为下拉菜单的默认值,如下所示: dropDownItems = [ 'Convert Text to Binary', 'Convert Binary to Text' ] clicked = tk.StringVar() clicked.set(dropDownItems[0]) dro

我正在尝试制作一个下拉菜单,当你按下按钮时,它会将输入的文本转换为二进制,或者将输入的二进制转换为文本,具体取决于你在下拉菜单中选择的内容。我制作了一个简单的下拉菜单,并将列表中的第一个值作为下拉菜单的默认值,如下所示:

dropDownItems = [
    'Convert Text to Binary',
    'Convert Binary to Text'
]

clicked = tk.StringVar()
clicked.set(dropDownItems[0])

dropDown = tk.OptionMenu(root, clicked, *dropDownItems)
dropDown.pack()
在程序快结束时,我这样做是为了当
单击.set(dropDownItems[0])
时,它会将文本转换为二进制。这是默认值,所以在我看来,这应该在程序运行时运行

if clicked.set(dropDownItems[0]):
    # myClick on the left is basically myClick(), but tkinter doesn't want the
    # two parentheses. Change the text to a variable checking what "mode" the
    # thing is in.
    myButton = tk.Button(root, text='Convert Text to Binary', command=myClick)
    myButton.pack(side=BOTTOM, pady=25)
我很困惑,因为自从我单击了
后,
dropDownItems[0]
是默认值。set(dropDownItems[0])
,所以当我运行程序时,上面的代码应该正在运行(我希望在运行时创建按钮,但没有创建)。抱歉,如果不清楚,以下是整个程序:

import tkinter as tk
import json
from tkinter import *

root = tk.Tk()
root.title('BitCoiner') # BinaryTextConverter > B.T.C > BitCoiner (bit as in data
                        # bit)(coiner as in... coiner)
root.iconbitmap('c:/Users/Duttas/Desktop/Coding/BinaryConverter/test.ico')
root.geometry('300x300')
#root.resizable(False, False) # Makes resizing the window impossible.

background = tk.PhotoImage(file='background.gif')
background_label = tk.Label(root, image=background)
background_label.place(x=0, y=0, relwidth=1, relheight=1)

root.update() # Ensures that when we call winfo_width that we won't get the
              # wrong width if the root (window) hasn't updated.
Banner = tk.Label(root, bg='grey' ,text='BitCoiner', width=root.winfo_width(), height=3)
Banner.pack()
#new below
dropDownItems = [
    'Convert Text to Binary',
    'Convert Binary to Text'
]

clicked = tk.StringVar()
clicked.set(dropDownItems[0])

dropDown = tk.OptionMenu(root, clicked, *dropDownItems)
dropDown.pack()
#new^

userInput = tk.Entry(root)
userInput.pack(expand = True, fill='both', padx=10, pady=10)

def binaryEncoder(s=''):
    return [bin(ord(c))[2:].zfill(8) for c in s] # c is character in this case.
                                                 # Ord is ASCII/Bin is Binary.
                                                 # zfill makes a wall of 8x 0s
                                                 # for it to be written over or
                                                 # as someone explained it --
                                                 # "padding" (binary is 8 digits/bit)

def myClick():
    s = userInput.get()
    myLabel = tk.Label(root, text=(binaryEncoder(s))) # Change 'text=' so it
                                                      # creates file with the
                                                      # binary/text data and when
                                                      # you click on it it copys
                                                      # to clipboard.
    root.withdraw()
    root.clipboard_clear()
    root.clipboard_append(binaryEncoder(s))
    root.update
    #with open('convertedBinary.json', 'w') as f:
        #json.dump(binaryEncoder(s), f)
    myLabel.pack()

if clicked.set(dropDownItems[0]):
    # myClick on the left is basically myClick(), but tkinter doesn't want the
    # two parentheses. Change the text to a variable checking what "mode" the
    # thing is in.
    myButton = tk.Button(root, text='Convert Text to Binary', command=myClick)
    myButton.pack(side=BOTTOM, pady=25)

root.mainloop()

请检查代码片段。我修改了你的按钮触发功能。 我也附上了截图。请检查这是否是您所需要的


是的,你修好了。非常感谢。
def myClick(arg=None):
    s=userInput.get()
    myLabel = tk.Label(root, text=(binaryEncoder(s))) #change 'text=' so it creates file with the binary/text data and when you click on it it copys to clipboard
    #root.withdraw()
    root.clipboard_clear()
    root.clipboard_append(binaryEncoder(s))
    root.update
    myLabel.pack()


myButton = tk.Button(root, text="Run", command=myClick)
myButton.pack(side=BOTTOM, pady=25)