def menu_button(self, title, action, diameter, center):
btn = ui.Button(
title=title,
action=action,
tint_color='white',
background_color=self.menu_color
)
btn.width = btn.height = diameter
btn.corner_radius = diameter/2
btn.center = center
self.menu.add_subview(btn)
# def menu_player_field(self, action, name, text, corner, width):
# (x,y) = corner
# fld = ui.TextField(
# action=action,
# name=name,
# text=text,
# tint_color='white',
# corner_radius=5,
# background_color=self.menu_color,
# frame=(x, y, width, 30)
# )
# self.menu.add_subview(fld)
python类TextField()的实例源码
def __init__(self, default_user_name='Name'):
self.name = 'Enter your username:'
self.background_color = 0.40, 0.80, 1.00
self.frame=(0, 0, 500, 500)
self.label = ui.Label(frame=(12, 100, 2000, 55))
self.label.text = 'What is your name?'
self.label.text_color = 'black'
self.label.font = ('Avenir-Black', 55)
self.add_subview(self.label)
self.text_field = ui.TextField(frame=(155, 175, 200, 32))
self.text_field.text = default_user_name
self.text_field.text_color = 'grey'
self.text_field.clear_button_mode = 'while_editing'
self.add_subview(self.text_field)
button = ui.Button(background_color='white',
frame=(360, 175, 75, 36),
image=ui.Image.named('ionicons-arrow-right-a-32'))
self.add_subview(button)
def __init__(self,frame=(0,0,300,32),name='dropdown', items=[]):
'''Create a dropdown view, with items in list.
items can be either an iterable, or a function returning an iterable.
the function can be interrupted if it checks .abort(), which is set when user selects a row, for expensive ops like os.walk.
pressing the dropdown button brings up the list, which can be aborted by selecting an item
'''
self.frame=frame
self.textfield=ui.TextField(frame=frame,name='textfield')
self.textfield.autocapitalization_type=ui.AUTOCAPITALIZE_NONE
self.textfield.autocorrection_type=False
self.button=ui.Button(name='button',bg_color=None)
self.add_subview(self.textfield)
self.add_subview(self.button)
h=frame[3]
self.button.frame=(self.width-32, h-32, 32,32)
self.button.image=ui.Image.named('ionicons-arrow-down-b-32')
self.button.action=self.open_finder
self.base=os.path.expanduser('~/Documents')
self._abort=False
self.items=items
self.button.flex='l'
self.textfield.flex='w'
def touch_ended(self,touch):
# dispatch whatever is under the touch
# for multitouch probably only want to execute when there are no active touches left.
# this method would need to clean out touches, but still keep info on the active gesture. when there are no active touches left, then kill the gesture
# for now.... just look under the touch, and call something appropriate.
# need to handle each ui type!
#print self.name, 'touch ended'
for s in self.subviews:
#probably need to check whether another view is on top...
if TouchDispatcher.hit(s,ui.convert_point(touch.location,self,s)):
if isinstance(s,ui.TextField):
#print '..textfield begin editing'
s.begin_editing()
#think about setting cursor.... HARD! but possible i think?
elif isinstance(s, ui.Button):
#print '..button launch'
s.action(s)
elif isinstance(s, TouchDispatcher):
# adjust touch location to subviews coordinates, then dispatch
# print '..touch end: dispatch: ', s.name
t=Touch(touch)
t.location=ui.convert_point(touch.location,self,s)
s.touch_ended(t)
def createView():
main=ui.View(frame=(0,0,576,576))
subpanel=ui.View(frame=(0,330,576,246))
subpanel2=ui.View(frame=(0,0,576,246))
slider=ui.Slider(frame=(10,10,150,44),name='slider1')
slider.stored=True
switch1=ui.Switch(frame=(10,66,70,44),name='switch1')
switch1.stored=True
switch2=ui.Switch(frame=(90,66,70,44)) #no name
switch2.stored=True
switch3=ui.Switch(frame=(10,120,70,44),name='switch3') #not stored
tf1=ui.TextField(frame=(10,180,250,50),name='textfield')
tf1.stored=True
main.add_subview(subpanel)
subpanel.add_subview(slider)
subpanel.add_subview(switch1)
subpanel.add_subview(switch2)
subpanel.add_subview(switch3)
subpanel.add_subview(tf1)
slider2=ui.Slider(frame=(10,10,150,44),name='slider1')
slider2.stored=True
subpanel2.add_subview(slider2)
main.add_subview(subpanel2)
return main
def __init__(self, initial_width, initial_height, *args, **kwargs):
super().__init__(*args, **kwargs)
self._initial_width = initial_width
self._initial_height = initial_height
self.frame = (10, 100, 275, 250)
self.background_color = 'white'
self.img_width = ui.TextField(frame=(10, 30, 100, 25),
keyboard_type=ui.KEYBOARD_DECIMAL_PAD,
text=str(initial_width),
action=self.update_height)
self.add_subview(self.img_width)
width_label = ui.Label(frame=(10, 5, 150, 25), text='Image Width')
self.add_subview(width_label)
self.img_height = ui.TextField(frame=(150, 30, 100, 25),
keyboard_type=ui.KEYBOARD_DECIMAL_PAD,
text=str(initial_height),
action=self.update_width)
self.add_subview(self.img_height)
height_label = ui.Label(frame=(150, 5, 150, 25), text='Image Height')
self.add_subview(height_label)
aspect_ratio = self._initial_width / self._initial_height
self.aspect = ui.TextField(frame=(70, 100, 150, 25),
text=str(aspect_ratio),
keyboard_type=ui.KEYBOARD_DECIMAL_PAD,
alignment=ui.ALIGN_CENTER,
action=self.updatefrom_aspectratio)
self.add_subview(self.aspect)
aspect_label = ui.Label(frame=(70, 75, 150, 25),
text='Aspect',
alignment=ui.ALIGN_CENTER)
self.add_subview(aspect_label)
self.save_button = ui.Button(frame=(175, 150, 50, 25),
title='Save',
action=self.save)
self.add_subview(self.save_button)
self.cancel_button = ui.Button(frame=(30, 150, 50, 25),
title='Cancel',
action=self.cancel)
self.add_subview(self.cancel_button)
def unitext(self, params):
self.unitexts.append([])
if self.kivy:
from kivy.uix.textinput import TextInput
self.unitexts[len(self.unitexts) - 1] = TextInput (
id = 'text' + str(len(self.unitexts) - 1),
size_hint_y = None,
size_hint_x = None,
height = params[3] * self.yratio,
width = params[2] * self.xratio,
text = params[4],
multiline = True,
font_size = 17.5 * self.yratio,
pos = (params[0] * self.xratio, params[1] * self.yratio))
self.root.add_widget(self.unitexts[len(self.unitexts) - 1])
else:
import ui
self.unitexts[len(self.unitexts) - 1] = ui.TextField(frame=
(params[0] * self.xratio, (600 - params[1] - params[3]) * \
self.yratio, params[2] * self.xratio, params[3] * self.yratio))
self.unitexts[len(self.unitexts) - 1].bordered = False
self.unitexts[len(self.unitexts) - 1].background_color = 'white'
self.unitexts[len(self.unitexts) - 1].font = ('<system>', 23 * \
self.xratio)
self.unitexts[len(self.unitexts) - 1].text = params[4]
self.root.add_subview(self.unitexts[len(self.unitexts) - 1])
def unitext(self, params):
self.unitexts.append([])
if self.kivy:
from kivy.uix.textinput import TextInput
self.unitexts[len(self.unitexts) - 1] = TextInput (
id = 'text' + str(len(self.unitexts) - 1),
size_hint_y = None,
size_hint_x = None,
height = params[3] * self.yratio,
width = params[2] * self.xratio,
text = params[4],
multiline = True,
font_size = 17.5 * self.yratio,
pos = (params[0] * self.xratio, params[1] * self.yratio))
self.root.add_widget(self.unitexts[len(self.unitexts) - 1])
else:
import ui
self.unitexts[len(self.unitexts) - 1] = ui.TextField(frame=
(params[0] * self.xratio, (600 - params[1] - params[3]) * \
self.yratio, params[2] * self.xratio, params[3] * self.yratio))
self.unitexts[len(self.unitexts) - 1].bordered = False
self.unitexts[len(self.unitexts) - 1].background_color = 'white'
self.unitexts[len(self.unitexts) - 1].font = ('<system>', 17.5 * \
self.yratio)
self.unitexts[len(self.unitexts) - 1].text = params[4]
self.root.add_subview(self.unitexts[len(self.unitexts) - 1])
def unitext(self, params):
self.unitexts.append([])
if self.kivy:
from kivy.uix.textinput import TextInput
self.unitexts[len(self.unitexts) - 1] = TextInput (
id = 'text' + str(len(self.unitexts) - 1),
size_hint_y = None,
size_hint_x = None,
height = params[3] * self.yratio,
width = params[2] * self.xratio,
text = params[4],
multiline = True,
font_size = 17.5 * self.yratio,
pos = (params[0] * self.xratio, params[1] * self.yratio))
self.root.add_widget(self.unitexts[len(self.unitexts) - 1])
else:
import ui
self.unitexts[len(self.unitexts) - 1] = ui.TextField(frame=
(params[0] * self.xratio, (600 - params[1] - params[3]) * \
self.yratio, params[2] * self.xratio, params[3] * self.yratio))
self.unitexts[len(self.unitexts) - 1].bordered = False
self.unitexts[len(self.unitexts) - 1].background_color = 'white'
self.unitexts[len(self.unitexts) - 1].font = ('<system>', 17.5 * \
self.yratio)
self.unitexts[len(self.unitexts) - 1].text = params[4]
self.root.add_subview(self.unitexts[len(self.unitexts) - 1])
def set_actions(self):
# method 2: traversing all subviews
for subview in self.subviews:
if isinstance(subview, ui.TextField):
subview.delegate = self
elif isinstance(subview, ui.Button):
if subview.name == 'say hi':
# method 3: button-specific action methods
subview.action = self.say_hi
else:
subview.action = self.button_pressed
def onNewInstrument(self,sender):
''' allow editing of new instrument based on current instrument'''
if model._InstrumentTuning:
notes = model._InstrumentTuning
else:
console.hud_alert('Please select an instrument as the base for new one','error',2)
return
mainViewShield.conceal()
numStrings = len(notes)
self.span = model._Span
self.octave = model._InstrumentOctave
self.textField.text = model._InstrumentName
self.octaveTextField.text = "{}".format(self.octave)
spinnerWidth = min(75,int(self.width/float(numStrings)))
space = self.width - numStrings*spinnerWidth
spacer = int(space/float(numStrings + 1))
self.notes = [note for note in notes]
self.octaves = [divmod(note,12)[0] for note in notes]
self.spinnerFactor = 0.17
for i in range(numStrings):
temp = Spinner(name="string{}".format(i),
initialValue = ccc['NOTE_NAMES'],
spinnerSize = (spinnerWidth,40),
fontSize = 16,
action=self.onSpinner,
limitAction=self.onSpinnerLimit
)
temp.pointer = self.notes[i] % 12
tempOctave = ui.TextField(name='octave{}'.format(i),
frame=(0,0,40,32),
)
tempOctave.text = "{}".format(self.octaves[i])
temp.position = (spacer+i*(spacer+spinnerWidth), int(self.spinnerFactor*self.height))
location = (spacer+i*(spacer+spinnerWidth),int(self.spinnerFactor*self.height+42),0.0,0.0)
tempOctave.frame = tuple(map(add,tuple(location),tuple(tempOctave.frame)))
self.add_subview(temp)
self.add_subview(tempOctave)
self.spinnerArray.append(temp)
self.octaveTextArray.append(tempOctave)
self.SpanSpinner = Spinner(name='EIspanSpinner',
initialValue = self.span,
limits=(2,self.span+2),
spinnerSize = (spinnerWidth,32),
fontSize = 16
)
self.SpanSpinner.position = (240,185)
self.add_subview(self.SpanSpinner)
self.tuningButton.action = self.playTuning
self.update_tuning_label()
self.hidden = False
self.bring_to_front()
def __init__(self, **kwargs):
super().__init__(**kwargs)
if 'background_color' not in kwargs:
self.background_color = 'white'
if 'frame' not in kwargs:
self.width = min(ui.get_window_size()[0] * 0.8, 700)
self.height = ui.get_window_size()[1] * 0.8
self._tableview = ui.TableView()
self._textfield = ui.TextField()
self._help_label = ui.Label()
self._datasource = None
self._handlers = None
self.shift_enter_enabled = True
self.did_select_item_action = None
tf = LayoutProxy(self._textfield)
self.add_subview(tf)
tf.layout.align_left_with_superview.equal = 8
tf.layout.align_top_with_superview.equal = 8
tf.layout.align_right_with_superview.equal = -8
tf.layout.height.equal = 31
tf.delegate = self
tv = LayoutProxy(self._tableview)
self.add_subview(tv)
tv.layout.align_left_to(tf).equal = 0
tv.layout.align_right_to(tf).equal = 0
tv.layout.top_offset_to(tf).equal = 8
tv.allows_selection = True
tv.allows_multiple_selection = False
hl = LayoutProxy(self._help_label)
self.add_subview(hl)
hl.layout.align_left_to(tv).equal = 0
hl.layout.align_right_to(tv).equal = 0
hl.layout.top_offset_to(tv).equal = 8
hl.layout.align_bottom_with_superview.equal = -8
hl.layout.height.max = 66
hl.font = ('<system>', 13.0)
hl.alignment = ui.ALIGN_CENTER
hl.text_color = (0, 0, 0, 0.5)
hl.number_of_lines = 2
if is_in_hardware_keyboard_mode:
tf.view.begin_editing()
self._register_key_event_handlers()