Kivy Python试图将同一屏幕分割成独特的部分
我有一个问题,我想有一列按钮下拉下来的左侧应用程序,每个按钮是一个不同的随机生成器。我希望下拉列表和按钮占据大约20-30%的屏幕空间,应用程序中/右侧的其余部分显示每个下拉列表包含的生成器的输出。目前我看到的是,一旦我添加了足够多的下拉列表,我到达了输出的位置,下拉列表就会将文本推到陌生的地方,反之亦然。我尝试将标签或下拉列表放入不同形式的布局中,并使用pos、pos_提示或Size_提示选项移动项目,但没有任何运气 我有一个尝试使用screenmanager的想法,但理想情况下,我希望这一切都在一个屏幕上,而不是跳转到多个屏幕。有没有办法将这两个部分分开,这样它们就不会相互影响 以下是我的python代码:Kivy Python试图将同一屏幕分割成独特的部分,python,python-3.x,layout,kivy,Python,Python 3.x,Layout,Kivy,我有一个问题,我想有一列按钮下拉下来的左侧应用程序,每个按钮是一个不同的随机生成器。我希望下拉列表和按钮占据大约20-30%的屏幕空间,应用程序中/右侧的其余部分显示每个下拉列表包含的生成器的输出。目前我看到的是,一旦我添加了足够多的下拉列表,我到达了输出的位置,下拉列表就会将文本推到陌生的地方,反之亦然。我尝试将标签或下拉列表放入不同形式的布局中,并使用pos、pos_提示或Size_提示选项移动项目,但没有任何运气 我有一个尝试使用screenmanager的想法,但理想情况下,我希望这一切
from kivy.app import App
#Builder allows us to call another file such as our kv file
from kivy.lang import Builder
#BoxLayout allows us to use this layout
from kivy.uix.boxlayout import BoxLayout
#Random allows us to use random choice and other random triggers
import random
#Allows us to trigger window size or make the app full screen
from kivy.core.window import Window
#Setting the size of the window that opens
Window.fullscreen = 'auto'
#Deciding if user can resize the window or not
from kivy.config import Config
Config.set('graphics', 'resizable', True)
#Importing a CSV
import csv
#Imports setting up a button with Kivy
from kivy.uix.button import Button
#Imports setting up a popup with kivy
from kivy.uix.popup import Popup
#Imports the kivy layout of Grid
from kivy.uix.gridlayout import GridLayout
class Generators(BoxLayout):
#Generates random LOUCHE choice for 7-9 rolls in Dungeon World
def louche(self, *args):
LOUCHE = ('LOCALITY: Something specifically related to the current environment happens.\nThe buildings now on fire. The ground collapses. It\'s flooding. Moonquake!' , 'OFFER: Offer a bargain, an extra, or a perk for a cost.\noffer a better position, with risk. Offer a temptation.', 'UNEXPECTED DANGER: Make something up or roll it up at random.\nTie it in if you want now or worry about how it fits in later', 'CALLBACK: Use something that they\'ve given you. A backstory element.\nAn off-handed comment. Gear. A character sheet aspect', 'HARM: Deal damage', 'END SOMETHING: End an ongoing effect, bonus, or fictional advantage. Take a \nresource away, something you possess, whether it\'s a piece of gear, \nan ability, or an ally')
self.ids.main_label.text = (str(random.choice(LOUCHE)))
#Female Name Generation
def Female_name_gen(self, *args):
#Declares the array variables of First_Name and Last_Name
First_Name = []
Last_Name = []
#Opens the csv file that contains the random names
with open(r'C:\Users\Aaron\Desktop\Names_List.csv') as f:
reader = csv.reader(f, skipinitialspace=True)
for col in reader:
#This verifies it doesn't get a blank or the column title and if it does it continues until it doesn't get a blank
if not col[1] or col[1] == "Female First Name": continue
else:
#This appends column 1 to First_Name
First_Name.append(col[1])
#This verifies it doesn't get a blank or the column title and if it does it continues until it doesn't get a blank
if not col[2]: continue
if not col[2] or col[2] == "Last Names": continue
else:
#This appends column 2 to Last_Name
Last_Name.append(col[2])
#Print a random first and last name for females
self.ids.Char_Name.text = 'Name: ' + random.choice(First_Name) + ' ' + random.choice(Last_Name)
#Male Name Generation
def Male_name_gen(self, *args):
First_Name = []
Last_Name = []
with open(r'C:\Users\Aaron\Desktop\Names_List.csv') as f:
reader = csv.reader(f, skipinitialspace=True)
for col in reader:
##This verifies it doesn't get a blank or the column title and if it does it continues until it doesn't get a blank
if not col[0] or col[0] == "Male First Name": continue
else:
#This appends column 0 to First_Name
First_Name.append(col[0])
##This verifies it doesn't get a blank or the column title and if it does it continues until it doesn't get a blank
if not col[2] or col[2] == "Last Names": continue
else:
#This appends column 2 to Last_Name
Last_Name.append(col[2])
#Print a random first and last name for Males
self.ids.Char_Name.text = 'Name: ' + random.choice(First_Name) + ' ' + random.choice(Last_Name)
#Clears what is showing on the main_label of the application
def clear(self, *args):
self.ids.main_label.text = ''
#Generic Character Generator
def Gen_Char_Gen(self, *args):
self.onButtonPress()
Class_Choice = ('Barbarian', 'Cleric', 'Druid', 'Fighter', 'Monk', 'Sorcerer', 'Wizard')
self.ids.class_label.text = 'Class: ' + (str(random.choice(Class_Choice)))
# On button press - Create a popup dialog with a label and a close button
def onButtonPress(self):
layout = GridLayout(cols=1, padding=10)
MaleNameButton = Button(text = "Click for Male Name Generation")
FemaleNameButton = Button(text = "Click for Female Name Generation")
layout.add_widget(MaleNameButton)
layout.add_widget(FemaleNameButton)
# Instantiate the modal popup and display
popup = Popup(title='Random Name Generator',
content=layout)
popup.open()
#Need a close for both buttons and send them to different name generators based on gender
# Dismiss causes the popup to go away. Release generates the call to the name generator specified
MaleNameButton.bind(on_press=popup.dismiss)
MaleNameButton.bind(on_release = self.Male_name_gen)
FemaleNameButton.bind(on_press=popup.dismiss)
FemaleNameButton.bind(on_press = self.Female_name_gen)
class app1(App):
def build(self):
return Builder.load_file('Tutorials/Kivy/Experimental.kv')
if __name__=="__main__":
app1().run()
以下是KV文件:
Generators:
BoxLayout: # left side of App
orientation: 'vertical' #Orientation is Vertical instead of horizontal
canvas:
Color:
rgba: 0, .0, 0, .0
Rectangle:
pos: self.pos
size: self.size
Button:
text: 'Generic Generators'
height: '50dp'
size_hint: 0.20, None
on_parent: drop1.dismiss()
on_release: drop1.open(self)
DropDown:
id: drop1
on_select: btn.text = '{}'.format(args[2])
Button:
text: 'LOUCHE'
size_hint_y: None
height: 45
on_press: root.clear()
on_release: root.louche()
Button:
text: "Random Name\n Generator"
size_hint_y: None
height: 45
on_press: root.clear()
on_release: root.onButtonPress()
Button:
text: "Generic Character Generator"
size_hint_y: None
height: 45
on_press: root.clear()
on_release: root.Gen_Char_Gen()
Button:
text: 'Scarlet Heroes'
height: '50dp'
size_hint: 0.20, None
on_parent: drop2.dismiss()
on_release: drop2.open(self)
DropDown:
id: drop2
on_select: btn.text = '{}'.format(args[1])
Button:
text: 'First Item'
size_hint_y: None
on_release: dropdown.select('First Item')
Button:
text: 'Drop 3'
height: '50dp'
size_hint: 0.20, None
on_parent: drop_content.dismiss()
on_release: drop_content.open(self)
DropDown:
id: drop_content
on_select: btn.text = '{}'.format(args[1])
Button:
text: 'First Item'
size_hint_y: None
on_release: dropdown.select('First Item')
Button:
text: 'Drop 4'
height: '50dp'
size_hint: 0.20, None
on_parent: drop4.dismiss()
on_release: drop4.open(self)
DropDown:
id: drop4
on_select: btn.text = '{}'.format(args[1])
Button:
text: 'First Item'
size_hint_y: None
on_release: dropdown.select('First Item')
Label: #Random Table Output label in the top center of the app
text: "Random Table Output"
font_size: 24
size_hint_y: .15
pos_hint: {'Center_x':.5, 'top':1}
Label: #Label for where Name Prints
id: Char_Name
text: " "
font_size: 24
size_hint: (None, None)
pos_hint: {"center_y": 0.4}
size: (1000, 100)
Label: #Label for class choice
id: class_label
text: " "
font_size: 24
size_hint: (None, None)
pos_hint: {"center_y": 0.4}
size: (1000, 0)
Label: #Starter label in the center of the app that disappears when the generators are activated
id: main_label
text: "Press a random generator on the side and output will display here."
font_size: 24
size_hint_y: .95
pos_hint: {'center_x':.5, 'top':.1}