使用python3和gtk3隐藏和显示小部件

时间:2017-05-16 10:41:24

标签: python gtk3

我正在编写一个用于通过gtk3界面上传图像的python脚本:

#!/usr/bin/python3

from imgurpython import ImgurClient 
import os
import notify2
import requests
import gi
notify2.init("kyimgur")

client_id = "xxxxx"
client_secret = "xxxxxxxxxxxxxxxxxx"
client = ImgurClient(client_id, client_secret)
auth_url = client.get_auth_url('pin')


import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class MyWindow(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self, title="Hello World")
        self.pin = ""
        self.client = client
        self.auth_widget = Gtk.VBox()
        self.auth_button = Gtk.LinkButton(auth_url, "Open authorization link")
        self.auth_widget.add(self.auth_button)
        self.auth_label = Gtk.Label("Paste the pincode here:")
        self.auth_widget.add(self.auth_label)
        self.paste_field = Gtk.Entry()
        self.paste_field.set_editable(True)
        self.pin_button = Gtk.Button("Submit pin")
        self.auth_widget.add(self.paste_field)
        self.auth_widget.add(self.pin_button)
        self.pin_button.connect("clicked", self.on_pin_button_clicked)
        self.main_widget = Gtk.VBox()
        self.watch_button = Gtk.Button("Watch a folder") 
        self.upload_img_button = Gtk.Button("Upload an image")
        self.upload_folder_button = Gtk.Button("Upload a folder")
        self.main_widget.add(self.watch_button)
        self.main_widget.add(self.upload_img_button)
        self.main_widget.add(self.upload_folder_button)
        self.main_widget.hide
        self.add(self.auth_widget)

    def on_pin_button_clicked(self, button):
        self.pin = self.paste_field.get_text()
        self.authorize(self.pin)

    def authorize(self, pin): 
        credentials = client.authorize(pin, 'pin')
        self.client.set_user_auth(credentials['access_token'], credentials['refresh_token'])
        self.remove(self.auth_widget)
        self.add(self.main_widget)


#    def upload_img(img_file):
#        img_file = "/home/kaiyin/Pictures/ar_satellite_closed/IMG_1036.jpg"
#        upload_res = self.client.upload_from_path(img_file, anon=False)
#        img_link = upload_res["link"]
#        os.system("echo '{}' | xsel -b".format(img_link))
#        note = notify2.Notification("Image uploaded to {}".format(img_link))
#        note.show()

win = MyWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()

这个想法是,当应用程序完成授权时,应该隐藏与auth相关的小部件,并且应该显示主要小部件。我在这里得到的是隐藏部分正在工作,但显示部分不是,即单击提交按钮时会显示一个空白窗口。

有什么建议吗?

0 个答案:

没有答案