微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Tkinter的:当选择某个下拉值,如何让一个按钮做别的事情?

如何解决Tkinter的:当选择某个下拉值,如何让一个按钮做别的事情?

我正在尝试制作一个下拉菜单,因此当您按下按钮时,它将根据您在下拉菜单中选择的内容将输入的文本转换为二进制或将二进制输入的文本转换为文本。我做了一个简单的下拉菜单,并将列表中的第一个值作为下拉菜单认值,如下所示:

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

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

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

在程序即将结束时,我这样做了,以便在clicked.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]是自clicked.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',*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(),pady=25)

root.mainloop()

解决方法

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

Bitcoiner

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)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。