我想使用base 64
并将编码结果打印到Gtk.Label
,这是我目前的代码:
from gi.repository import Gtk
import base64
class classInputs(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Inputs try window")
self.set_border_width(10)
self.set_size_request(200,100)
# Layout
vertical_box = Gtk.Box(orientation = Gtk.Orientation.VERTICAL, spacing = 8)
self.add(vertical_box)
#userName input
self.username = Gtk.Entry()
self.username.set_text("")
vertical_box.pack_start(self.username, True, True, 0)
#Password input
self.password = Gtk.Entry()
self.password.set_text("aaaaaaaaaaa")
self.password.set_visibility(False)
vertical_box.pack_start(self.password, True, True, 0)
#create login botton here
self.button = Gtk.Button(label="click me to login")
self.button.connect("clicked", self.function)
vertical_box.pack_start(self.button, True, True, 0)
#create label
self.label= Gtk.Label()
vertical_box.pack_start(self.label, True, True, 0)
#function of the button here bech ki tenzel 3al button y7ot lencode fel input 1
def function (self, widget):
self.label.set_text(base64.b64encode(b'data to be encoded'))
shinerghost = classInputs()
shinerghost.connect("delete-event",Gtk.main_quit)
shinerghost.show_all()
Gtk.main()
关于如何实现这一目标的任何想法?
答案 0 :(得分:0)
假设您想要密码字段的值非常接近,您唯一忘记的就是获取密码字段的值并将其放入编码中,如下所示:
def function (self, widget):
# Get the value from the input field
string = self.password.get_text()
# Turn the string into bytes
byte_string = string.encode('utf-8')
# Encode the string
encoded_string = base64.b64encode(byte_string )
# Set the label text
self.label.set_text(encoded_string)