2017-12-27 10 views
1

これは私の最初のKivyアプリです。驚くべきことに、単純なのでクリップボードにコピー/ペーストボタンのテキストに関するドキュメントはありませんでしたが、ValueError埋め込まれたヌル文字。クリップボードにKivyコピーボタンのテキスト

ボタンが最近ハッシュされたテキストのテキストを生成し、まだバイト文字列に含まれていたためだと思っていましたが、デコード時に既にデコードされて状態文字列にデコード属性がない場合に機能します。かかわらず、データの、私は問題を発見したと信じ更新 ** https://kivy.org/docs/api-kivy.core.clipboard.html#

:私はすべての答えは私に

kivyクリップボードドキュメントを見つめてきた私のコードにしている場合、「遊ぶ」ために、事前に謝罪しますクリップボードの関数に渡される型の値エラーです。クリップボード "clipboard_winctypes.py"のkivyファイルを見て、put()関数の下で関数msvcrt.wcscpy_s()が呼び出されました。これをコメントアウトすると、クリップボードはボタンのテキストをコピーしますが、奇数のものは⫐ᵄƅ

また、テキストがtext + = u'x00 'に設定されているput()関数の下でコメントアウトされていますmsvcrt.wscpy_s()はエラーなしで実行されるようにコメントアウトされていますが、クリップボードには何もコピーされませんが、msvcrtはctypes.cdll.msvcrtのオブジェクトで、ここからはどこにも移動しません

clipboard_wyname PY:

''' 
Clipboard windows: an implementation of the Clipboard using ctypes. 
''' 

__all__ = ('ClipboardWindows',) 

from kivy.utils import platform 
from kivy.core.clipboard import ClipboardBase 

if platform != 'win': 
    raise SystemError('unsupported platform for Windows clipboard') 

import ctypes 
from ctypes import wintypes 
user32 = ctypes.windll.user32 
kernel32 = ctypes.windll.kernel32 
msvcrt = ctypes.cdll.msvcrt 
c_char_p = ctypes.c_char_p 
c_wchar_p = ctypes.c_wchar_p 


class ClipboardWindows(ClipboardBase): 

    def get(self, mimetype='text/plain'): 
     GetClipboardData = user32.GetClipboardData 
     GetClipboardData.argtypes = [wintypes.UINT] 
     GetClipboardData.restype = wintypes.HANDLE 

     user32.OpenClipboard(user32.GetActiveWindow()) 
     # 1 is CF_TEXT 
     pcontents = GetClipboardData(13) 
     if not pcontents: 
      return '' 
     data = c_wchar_p(pcontents).value.encode(self._encoding) 
     user32.CloseClipboard() 
     return data 

    def put(self, text, mimetype='text/plain'): 
     text = text.decode(self._encoding) # auto converted later 
     text += u'\x00' 

     SetClipboardData = user32.SetClipboardData 
     SetClipboardData.argtypes = [wintypes.UINT, wintypes.HANDLE] 
     SetClipboardData.restype = wintypes.HANDLE 

     GlobalAlloc = kernel32.GlobalAlloc 
     GlobalAlloc.argtypes = [wintypes.UINT, ctypes.c_size_t] 
     GlobalAlloc.restype = wintypes.HGLOBAL 

     CF_UNICODETEXT = 13 

     user32.OpenClipboard(user32.GetActiveWindow()) 
     user32.EmptyClipboard() 
     hCd = GlobalAlloc(0, len(text) * ctypes.sizeof(ctypes.c_wchar)) 
     msvcrt.wcscpy_s(c_wchar_p(hCd), len(text), c_wchar_p(text)) 
     SetClipboardData(CF_UNICODETEXT, hCd) 
     user32.CloseClipboard() 

    def get_types(self): 
     return ['text/plain'] 

cry_hash.py

from kivy.app import App 
from kivy.uix.boxlayout import BoxLayout 
from kivy.uix.togglebutton import ToggleButton 
from kivy.uix.label import Label 
import hashlib 


class Hasher: 
    def __init__(self, to_hash, hash_alg, hash_length): 
     if to_hash != type(bytes): 
      self.to_hash = bytes(to_hash, encoding='utf-8') 
     else: 
      self.to_hash = to_hash 
     self.hash_alg = hash_alg 
     self.hash_length = int(hash_length) 

    def create_hash(self): 
     hash_object = hashlib.new(self.hash_alg) 
     hash_object.update(self.to_hash) 
     result = hash_object.hexdigest()[:self.hash_length] 
     del hash_object 
     return result 


class LabelBackground(Label): 
    pass 


class CryHashWidgetBoxLayout(BoxLayout): 
    def get_hash(self, user_hash, hash_length): 
     tb = next((t for t in ToggleButton.get_widgets('hash_type') if t.state == 'down'), None) 
     hash_alg = tb.text if tb else None 
     krypt_tool = Hasher(user_hash, hash_alg, hash_length) 
     hashed_input = krypt_tool.create_hash() 

     self.ids.hash_return.text = hashed_input 

    def reset(self, text_reset): 
     incoming = text_reset 
     del incoming 
     incoming = '' 
     self.ids.hash_return.text = incoming 


class CryHashApp(App): 
    def build(self): 
     return CryHashWidgetBoxLayout() 


if __name__ == '__main__': 
    CryHashApp().run() 

KVファイル:cryhash.kv私は周りの仕事を見つけた

#File name: cry_hash.py 
#:import utils kivy.utils 
#:import Clipboard kivy.core.clipboard.Clipboard 


<ToggleButton>: 
    background_color: utils.get_color_from_hex('#E00000') 

<TextInput>: 
    background_color: utils.get_color_from_hex('#5F9B9F') 

<Label> 
    font_name: 'fonts/arialbd.ttf' 

<CryHashWidgetBoxLayout>: 
    orientation: 'vertical' 

    Label: 
     font_name: 'fonts/welga.ttf' 
     color: utils.get_color_from_hex('#E00000') 
     text: 'Welcome to Cry Hash!' 
     font_size: 80 

    Button: 
     id: hash_return 
     background_color: utils.get_color_from_hex('#F15E92') 
     font_size: 40 
     text: '' 
     on_release: 
      Clipboard.copy(hash_return.text) 

    BoxLayout: 
     orientation: 'horizontal' 
     BoxLayout: 
      orientation: 'vertical' 

      Label: 
       id: bg_hash 
       color: utils.get_color_from_hex('#E00000') 
       text: 'Enter text to hash' 

      TextInput: 
       id: user_hash 
       multiline: False 
       text: '' 


      Label: 
       id: bg_length 
       color: utils.get_color_from_hex('#E00000') 
       text: 'Enter length' 

      TextInput: 
       id: get_hash_length 
       multiline: False 
       text: '10' 

      Button: 
       id: get_data 
       background_color: utils.get_color_from_hex('#1900FF') 
       text: 'get hash!' 
       on_release: root.get_hash(user_hash.text, get_hash_length.text) 

     BoxLayout: 
      orientation: 'vertical' 

      ToggleButton: 
       id: SHA256 
       text: 'SHA256' 
       state: 'down' 
       group: 'hash_type' 

      ToggleButton: 
       id: SHA512 
       text: 'SHA512' 
       group: 'hash_type' 

      ToggleButton: 
       id: SHA1 
       text: 'SHA1' 
       group: 'hash_type' 

      ToggleButton: 
       id: MD5 
       text: 'MD5' 
       group: 'hash_type' 

答えて

0

せてください誰かがkivyコード内の真の解決策を見つけることができれば、私は、それは単にKivyフレームワークのバグであると考えてい私が知っている、そうでなければ、私は単純にpyperclipをインポートし、.pyファイルにpyperclipコピー関数を作成し、.kvファイルの関数を呼び出しました。完璧に動作します!

cry_hash.py: 
from kivy.app import App 
from kivy.uix.boxlayout import BoxLayout 
from kivy.uix.togglebutton import ToggleButton 
from kivy.uix.label import Label 
import hashlib 
import pyperclip 

class Hasher: 
    def __init__(self, to_hash, hash_alg, hash_length): 
     if to_hash != type(bytes): 
      self.to_hash = bytes(to_hash, encoding='utf-8') 
     else: 
      self.to_hash = to_hash 
     self.hash_alg = hash_alg 
     self.hash_length = int(hash_length) 

    def create_hash(self): 
     hash_object = hashlib.new(self.hash_alg) 
     hash_object.update(self.to_hash) 
     result = hash_object.hexdigest()[:self.hash_length] 
     del hash_object 
     return result 


class LabelBackground(Label): 
    pass 


class CryHashWidgetBoxLayout(BoxLayout): 
    def get_hash(self, user_hash, hash_length): 
     tb = next((t for t in ToggleButton.get_widgets('hash_type') if t.state == 'down'), None) 
     hash_alg = tb.text if tb else None 
     krypt_tool = Hasher(user_hash, hash_alg, hash_length) 
     hashed_input = krypt_tool.create_hash() 
     self.ids.hash_return.text = str(hashed_input) 

    def reset(self, text_reset): 
     incoming = text_reset 
     del incoming 
     incoming = '' 
     self.ids.hash_return.text = incoming 

    def copy_text(self, text): 
     pyperclip.copy(text) 


class CryHashApp(App): 
    def build(self): 
     return CryHashWidgetBoxLayout() 


if __name__ == '__main__': 
    CryHashApp().run() 

cryhash.kv:

#File name: cry_hash.py 
#:import utils kivy.utils 
#:import Clipboard kivy.core.clipboard.Clipboard 


<ToggleButton>: 
    background_color: utils.get_color_from_hex('#E00000') 

<TextInput>: 
    background_color: utils.get_color_from_hex('#5F9B9F') 

<Label> 
    font_name: 'fonts/arialbd.ttf' 

<CryHashWidgetBoxLayout>: 
    orientation: 'vertical' 

    Label: 
     font_name: 'fonts/welga.ttf' 
     color: utils.get_color_from_hex('#E00000') 
     text: 'Welcome to Cry Hash!' 
     font_size: 80 

    Button: 
     id: hash_return 
     background_color: utils.get_color_from_hex('#F15E92') 
     font_size: 40 
     text: '' 
     on_release: root.copy_text(hash_return.text) 

    BoxLayout: 
     orientation: 'horizontal' 
     BoxLayout: 
      orientation: 'vertical' 

      Label: 
       id: bg_hash 
       color: utils.get_color_from_hex('#E00000') 
       text: 'Enter text to hash' 

      TextInput: 
       id: user_hash 
       multiline: False 
       text: '' 


      Label: 
       id: bg_length 
       color: utils.get_color_from_hex('#E00000') 
       text: 'Enter length' 

      TextInput: 
       id: get_hash_length 
       multiline: False 
       text: '10' 

      Button: 
       id: get_data 
       background_color: utils.get_color_from_hex('#1900FF') 
       text: 'get hash!' 
       on_release: root.get_hash(user_hash.text, get_hash_length.text) 

     BoxLayout: 
      orientation: 'vertical' 

      ToggleButton: 
       id: SHA256 
       text: 'SHA256' 
       state: 'down' 
       group: 'hash_type' 

      ToggleButton: 
       id: SHA512 
       text: 'SHA512' 
       group: 'hash_type' 

      ToggleButton: 
       id: SHA1 
       text: 'SHA1' 
       group: 'hash_type' 

      ToggleButton: 
       id: MD5 
       text: 'MD5' 
       group: 'hash_type' 
関連する問題