-2

I want to use base 64 and print the encode result into Gtk.Label, this is my code so far:

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()

Any ideas on how I can achieve that?

Dimitris Fasarakis Hilliard
  • 150,925
  • 31
  • 268
  • 253

1 Answers1

0

Assuming you want to encode the value of the password field you were pretty close, the only thing you forgot was to get the value of the password field and put it into the encode like this:

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)
B8vrede
  • 4,432
  • 3
  • 27
  • 40
  • actually i try it but i got this problem : /usr/bin/python3.4 /home/muhammad/PycharmProjects/PyGtk/inputs.py Traceback (most recent call last): File "/home/muhammad/PycharmProjects/PyGtk/inputs.py", line 57, in function encoded_string = base64.b64encode(string) File "/usr/lib/python3.4/base64.py", line 62, in b64encode encoded = binascii.b2a_base64(s)[:-1] TypeError: 'str' does not support the buffer interface – shiner spy Feb 04 '16 at 11:18
  • [++++++] then here my code : def function (self, widget): string = self.password.get_text() encoded_string = base64.b64encode(string) self.label.set_text(encoded_string) – shiner spy Feb 04 '16 at 11:19
  • Oh I didn't realize that you were on python 3.4, I tested the code on 2.7 which worked fine :) Let me check this for you. – B8vrede Feb 04 '16 at 11:23
  • I just added a extra line to the answer, it turns out that in 3.+ you need to encode the string first to get the byte string. http://stackoverflow.com/questions/7585435/best-way-to-convert-string-to-bytes-in-python-3 – B8vrede Feb 04 '16 at 11:28