Search code examples
pythonpopupkivyprettytable

Popup and the corrupted table showing in kivy


I'm a newbie in Python and Kivy as well, so I have some trouble.

When I use kivy popup with showing the table (using "PrettyTable" module) I get broken view of this table.

Screenshot

My python code:

  from kivy.app import App
  from kivy.uix.boxlayout import BoxLayout
  from kivy.uix.popup import Popup
  from kivy.uix.button import Button
  from kivy.uix.widget import Widget
  from prettytable import PrettyTable

class GeneralForm(BoxLayout):
   def RUN(self):

      def TABLE():
           x = PrettyTable(["City name", "Area", "Population"])
           x.align["City name"] = "l" # Left align city names
           x.padding_width = 1 # One space between column edges and contents (default)
           x.add_row(["Adelaide",1295, 1158259])
           x.add_row(["Brisbane",5905, 1857594])
           return str(x)


      popup = Popup(title='Test popup', content=Label(text=TABLE()), auto_dismiss=False)
      popup.open()


class TimeTable(App):
   def build(self):
       return GeneralForm()

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

My .kv code:

<GeneralForm>:
   orientation: "vertical"
   BoxLayout:
       Button:
           id: but
           text: "Show!"
           on_press: root.RUN()

Solution

  • Your problem is that the default label font is not a monospace font, where every character has the same width. You can instead set the font_name property to one that is. Possibly just 'DroidSansMono' will work, using the mono font kivy bundles.