简体   繁体   中英

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]

Issue: Text is not displayed. Adding new text to the label causes the word pause to get displayed with different font sizes.

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"

Upon typing something in the text box, sometimes some wierd image gets displayed. Also text_validate_unfocus: False does not prevent text box from unfocusing when enter key is pressed

Edit: Whole code: 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

(Btw, I am using qpython-kivy) Issue in brief: The text input(id:cmdbox) on editing sometimes displays wierd images in place of text. Also, the label(id:historybox) does not display the correct text(it only displays "pause" in different font size each time)

Edit2: Finally got some images. https://www.dropbox.com/sh/i6t192ujys2hivz/AACPR5Sgb72Mv8M7gB3DiGmNa?dl=0

For the first issue, in the __init__ of your screen you are replacing the text of your label

For the second, I don't know if that property exists in kivy If you want to keep the focus in your TextInput Try this:

...
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
...

And I can't say anything about the weird images because I don't get them

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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