Kivy标签小部件问题

时间:2017-12-11 01:27:29

标签: python kivy

ScrollView:
  id: historyscroll
  size_hint: 1, 0.925
  pos_hint: {"x": 0, "top": 1}
  Label:
    id: historybox
    text: "start"
    size_hint: 1, None 
    size_hint_y: None
    height: self.texture_size[1]

问题: 文本不显示。向标签添加新文本会导致单词“暂停”以不同的字体大小显示。

Issue2:

TextInput:
  text: "localhost:"
  size_hint: 1, 0.06
  pos_hint: {"x": 0, "y": 0}
  id: cmdbox
  multiline: False
  text_validate_unfocus:False
  font_size: "20sp"

在文本框中输入内容后,有时会显示一些奇怪的图像。 text_validate_unfocus:False也不会阻止文本框在按下回车键时失焦

编辑: 整码: main.py:

#-*-coding:utf8;-*-
#qpy:3
#qpy:kivy

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen, NoTransition
from kivy.clock import Clock
from kivy.core.window import Window
from game import Game

class Sm(ScreenManager):
  pass

class GameScreen(Screen):
  def __init__(self, **kwargs):
    super(GameScreen, self).__init__(**kwargs) 
    self.game = Game()
    self.add_widget(self.game)
    self.ids.cmdbox.bind(on_text_validate=self.cmdreturn)
    self.ids.cmdbox.bind(focus=self.cmdfocus)
    self.ids.historybox.text=" "*(Window.width*75/1048)
    #Clock.schedule_interval(self.render, 0.5) 
  def cmdreturn(self, args):
    self.cmd = self.ids.cmdbox.text
    #self.ids.historybox.insert_text("\n"+self.cmd)
    self.ids.historybox.text += "\n" + self.cmd
    self.cmdexecute(self.cmd.split(str(self.game.current_)+":")[1])  
    self.ids.cmdbox.text = str(self.game.current_) + ":"
  def cmdexecute(self, cmd):
    print(cmd)
    if cmd == "fill":
      self.ids.historybox.text+="\nfill"*30
    if cmd == "clear":
      self.ids.historybox.text= " "*(Window.width*75/1048)
    if cmd == "ls":
      self.ids.historybox.text= "\n"+str(self.game.current.folders.keys())
  def cmdfocus(self, instance, value):
    if value:
      self.ids.cmdbox.pos_hint={"x":0, "y":0.45}
      self.ids.historyscroll.size_hint=(1, 0.475)
    else:
      self.ids.cmdbox.pos_hint={"x":0, "y":0}
      self.ids.historyscroll.size_hint=(1, 0.925)

class MenuScreen(Screen):
  pass

class PauseScreen(Screen):
  pass

class OptionsScreen(Screen):
  pass

class GameApp(App):
  def build(self):
    sm = Sm()
    sm.add_widget(MenuScreen(name="menu"))
    sm.add_widget(GameScreen(name="game"))
    sm.add_widget(PauseScreen(name="pause"))
    sm.add_widget(OptionsScreen(name="options"))
    sm.transition = NoTransition()
    return sm

GameApp().run()

game.kv:

#kivy 1.10.0

<GameScreen>:
  id:gscreen
  FloatLayout: 
    Button:
      text:"pause"
      size_hint:(0.15, 0.05)
      pos_hint:{"right":0.98, "top":0.98}
      on_press:root.manager.current="pause"
    ScrollView:
      id: historyscroll
      size_hint: 1, 0.925
      pos_hint: {"x": 0, "top": 1}
      Label:
        id: historybox
        #readonly: True
        text: "start"
        size_hint: 1, None 
        size_hint_y: None
        height: self.texture_size[1]
        #height: max(self.minimum_height, historyscroll.height)
        #multiline: True
        #foreground_color: (1,1,1,1)
        #background_color: (255,255,255,0)
        #font_size: "17sp"
        #halign: "left"
        #text_size:(self.width, "None")
    TextInput:
      text: "localhost:"
      size_hint: 1, 0.06
      pos_hint: {"x": 0, "y": 0}
      id: cmdbox
      multiline: False
      text_validate_unfocus:False
      font_size: "20sp"

<MenuScreen>:
  FloatLayout:
    Button:
      text:"start"
      size_hint:(0.3, 0.1)
      pos_hint:{"center_x":0.5, "center_y":0.61}
      on_press:root.manager.current="game"
    Button:
      text:"options"
      size_hint:(0.3, 0.1)
      pos_hint:{"center_x":0.5, "center_y":0.5}
      on_press:root.manager.current="options"
    Button:
      text:"exit"
      size_hint:(0.3, 0.1)
      pos_hint:{"center_x":0.5, "center_y":0.39}
      on_press:quit

<OptionsScreen>:
  FloatLayout:
    Button:
      text:"back"
      size_hint:(0.3, 0.1)
      pos_hint:{"center_x":0.5, "center_y":0.5}
      on_press:root.manager.current="menu"


<PauseScreen>:
  FloatLayout:
    Button:
      text:"back"
      size_hint:(0.3, 0.1)
      pos_hint:{"center_x":0.5, "center_y":0.495}
      on_press:root.manager.current="game"
    Button:
      text:"exit"
      size_hint:(0.3, 0.1)
      pos_hint:{"center_x":0.5, "center_y":0.605}
      on_press:root.manager.current="menu"

game.py:

from kivy.uix.widget import Widget
from random import randint

class Game(Widget):
  def __init__(self, **kwargs):
    super(Game, self).__init__(**kwargs) 
    self.localhost = "5255.7611"
    self.internet = Internet(self.localhost)
    self.current_ = "localhost"
    self.current = self.internet.links[self.localhost.split(".")[0]].links[self.localhost.split(".")[1]]

class Internet:
  def __init__(self, localhost):
    self.links = {}
    self.links[str(localhost)[:4]] = Router(str(localhost)[:4], self)
    self.links[str(localhost)[:4]].islocal(localhost)
  def Crouter(self):
    tmp = str(randint(1000, 9999))
    if not str(tmp) in self.links:
      self.links[str(tmp)] = Router(tmp, self)
    else: self.Crouter

class Computer:
  def __init__(self, ip, router):
    self.ip = ip
    self.router = router
    self.islocal = False
    self.folders = {"programs":Folder("programs",[], {}),
                    "downloads":Folder("downloads",[], {})}

class Folder:
  def __init__(self, name, content, data):
    self.content = content
    self.data = data

class File:
  def __init__(self, content, data):
    self.content = content
    self.data = data

class Router:
  def __init__(self, ip, internet):
    self.ip = ip
    self.internet = internet
    self.links = {}
  def Ccomputer(self):
    tmp = str(randint(1000, 9999))
    if not str(tmp) in self.links:
      self.links[str(tmp)] = Computer(str(tmp)+self.ip, self)
    else: self.Ccomputer
  def islocal(self, localhost):
    self.links[str(localhost)[5:]] = Computer(str(localhost), self)
    self.links[str(localhost)[5:]].islocal = True

(顺便说一下,我正在使用qpython-kivy) 问题简述: 编辑时的文本输入(id:cmdbox)有时会显示奇怪的图像来代替文本。 此外,标签(id:historybox)不会显示正确的文本(它只显示&#34;暂停&#34;每次都使用不同的字体大小)

EDIT2: 终于得到了一些图片。 https://www.dropbox.com/sh/i6t192ujys2hivz/AACPR5Sgb72Mv8M7gB3DiGmNa?dl=0

1 个答案:

答案 0 :(得分:0)

对于第一个问题,在屏幕的__init__中,您要替换标签文字

对于第二个,我不知道该属性是否存在于kivy中如果您想将焦点保持在TextInput试试这个:

...
class GameScreen(Screen):
    def __init__(self, **kwargs):
        super(GameScreen, self).__init__(**kwargs) 
        self.game = Game()
        self.add_widget(self.game)
        self.ids.cmdbox.bind(on_text_validate=self.cmdreturn)
        self.ids.cmdbox.bind(focus=self.cmdfocus)
        #self.ids.historybox.text=" "*(Window.width*75/1048)
        #Clock.schedule_interval(self.render, 0.5) 

    def cmdreturn(self, args):
        self.cmd = self.ids.cmdbox.text
        #self.ids.historybox.insert_text("\n"+self.cmd)
        self.ids.historybox.text += "\n" + self.cmd
        self.cmdexecute(self.cmd.split(str(self.game.current_)+":")[1])
        self.ids.cmdbox.text = str(self.game.current_) + ":"
        Clock.schedule_once(self.refocus)

    def refocus(self, *args):
        self.ids.cmdbox.focus = True
...

我不能说奇怪的图像,因为我没有得到它们