我已经创建了一个下拉列表,并且不希望它遍及整个屏幕。所以我试图设置max_height值,但我只能放入绝对值。相反,我希望它像在使用size_hint一样在屏幕上调整大小。
我也试图用kv lang传递它。创建一个DropDown类并将max_height设置为(root.height -20),但是由于它尝试获取DropDown类而不是Screen的高度而无法正常工作。
这是我的代码:
def loss_fun(model, image_batch, label_batch, reg_loss_coef=1e-5):
main_loss = my_another_loss_fun(image_batch, label_batch)
reg_sum = 1e-5
for layer in model.layer:
is_trainable = layer.trainable
is_weight = is_trainable and (type(layer) == tf.keras.layers.Conv2D) #model-dependent check here
if is_weight:
for var in layer.variables:
reg_sum += tf.math.reduce_sum(tf.math.square(var)) * reg_loss_coef
return main_loss + reg_sum
model = tf.keras.applications.inception_resnet_v2.InceptionResNetV2(weights=None)
opt = tf.train.AdamOptimizer(0.01, epsilon=1e-5)
ds_tf = tf.data.Dataset.something()
for im_b, label_b in ds_tf:
with tf.GradientTape() as tape:
curr_loss = loss_fun(model, im_b, label_b)
with tape.stop_recording():
grads = tape.gradient(curr_loss, model.trainable_variables)
opt.apply_gradients(zip(grads, model_vars),
global_step=tf.train.get_or_create_global_step())
对于kv文件:
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
from kivy.uix.textinput import TextInput
from kivy.properties import ObjectProperty
from kivy.uix.dropdown import DropDown
from kivy.uix.button import Button
class MainWindow(Screen):
min = ObjectProperty(None)
seg = ObjectProperty(None)
def __init__(self, **kw):
super().__init__(**kw)
self.min_down = DropDown()
self.sec_down = DropDown()
for x in range(61):
btn_min = Button(text=str(x), size_hint_y=None, height=44)
btn_min.bind(on_release=lambda btn: self.minute(btn.text))
btn_min.bind(on_release=lambda dismiss: self.min_down.dismiss())
self.min_down.add_widget(btn_min)
self.min_down.auto_width = False
self.min_down.max_height = 100
def minute(self, texto):
self.min.text = texto
class NumericInput(TextInput):
def insert_text(self, string, from_undo=False):
new_text = self.text + string
self.input_filter = 'int'
if new_text != "":
try:
if int(new_text) >= 0:
self.text = new_text
if int(new_text) > 60:
self.text = "60"
if len(new_text) > 2:
self.text = new_text[:-1]
except ValueError:
TextInput.insert_text(self, "", from_undo=from_undo)
class WindowManager(ScreenManager):
pass
kv = Builder.load_file("teste.kv")
class TesteApp(App):
def build(self):
return kv
if __name__ == "__main__":
TesteApp().run()
答案 0 :(得分:1)
在您的MainWindow
类__init__()
中,您可以替换
self.min_down.max_height = 100
使用
self.bind(size=self.resizing)
并向该类添加resizing()
方法:
def resizing(self, screen, new_size):
self.min_down.max_height = new_size[1] * 0.5
self.min_down.width = new_size[0] / 10
# the child of the DropDownList is a GridLayout that contains the buttons
for btn in self.min_down.children[0].children:
btn.width = self.min_down.width
只要max_height
更改大小,这就会调整MainWindow
属性。