繁体   English   中英

Kivy标签小部件问题

[英]Kivy label widget issue

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]

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

问题2:

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不会防止按下Enter键时文本框失去焦点

编辑:整个代码: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)有时会显示奇怪的图像代替文本。 另外,label(id:historybox)不会显示正确的文本(每次仅以不同的字体大小显示“暂停”)

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

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

第二,我不知道该属性是否存在于基维中。如果要将焦点保持在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
...

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM