【发布时间】:2021-01-21 22:03:13
【问题描述】:
我的第一个目标是通过使用 .kv 文件中的工厂函数在弹出部分的按钮单击时使用 id:delft 打开选项卡 .(button id: buttontx) 当我点击按钮时,我收到这个错误: on_press: Factory.MyLayout().switch_to(MyLayout .ids.delft) NameError: name 'MyLayout' 未定义。
我的第二个目标是从弹出 .kv 创建数据库并在选项卡中显示名称。然而我无法启动它,因为我正在寻找解决我主要问题的方法。在下方,您可能会看到我的 .py 和 .kv 代码。
.PY
from kivy.app import App
from kivy.clock import Clock
from kivy.lang import Builder
from kivy.uix.popup import Popup
from kivy.uix.tabbedpanel import TabbedPanel
import time
import smtplib
import psycopg2
Builder.load_file('tabs.kv')
def sendmail():
username = 'kASASD'
subject = "SAAT 11"
password = 'ddsa'
body = 'DENEME'
msg = "\r\n".join([
"From: " + username,
"To: " + username,
"Subject: " + subject,
"",
body
])
server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
server.login(username, password)
print('sending')
server.sendmail(username, username, msg.encode("utf-8", errors="ignore"))
server.quit()
print('sent')
class MyLayout(TabbedPanel):
def updates(self, *args):
# saat, dakika saniye
self.ids.saat.text = time.strftime("%H:%M:%S")
# gün ay yıl gün
self.ids.tarih.text = time.strftime("%d %B %Y %A")
# 23de mail at
if time.strftime("%H:%M:%S") == "23:03:00":
sendmail()
class AwesomeApp(App):
def build(self):
t = MyLayout()
Clock.schedule_interval(t.updates, 1)
return t
if __name__ == '__main__':
AwesomeApp().run()
#:import Factory kivy.factory.Factory
<MyPopup@Popup>
# pop up kendine sadece 1 tane widget alır. Birkaç şey koymak istiyorsan. Boxlayout falan yapıp içine
#atman gerekiyor.
# kutu dışına dokununca kaybolsun mu? - hayır
auto_dismiss: False
size_hint: .4, .4
#top .9 = yukarının %90'nında dursun diye. x: saga sola
pos_hint: {"center_x": 0.5, "top": .8}
title: "Herhangi bir veri yok. Veri girin"
BoxLayout:
orientation: "vertical"
size: root.width, root.height
Label:
text: "Herhangi bir veri yok. Veri girin"
text: "Meta girin"
size_hint: 1, .2
TextInput:
id: table_input
text: "Metanız"
size_hint: 1, .2
font_size: 20
BoxLayout:
orientation: "horizontal"
size: root.width, root.height
size_hint: 1, .2
Button:
text: "Oluştur"
font_size: 20
id: create_db
Button:
id: buttontx
text: "Vazgeç"
font_size: 20
on_press: Factory.MyLayout().switch_to(MyLayout.ids.delft)
<MyLayout@TabbedPanel>
do_default_tab: True
default_tab: delft
#orantısal küçültür
size_hint: .99, .99
#ekranın ortasına bütün kareyi koyar
pos_hint: {'center_x': .5, 'center_y': .5}
# tabları ortalar - bottom tabları alta atar left_top, left_mid, left_bottom, top_left, top_mid, top_right,
#right_top, right_mid, right_bottom, bottom_left, bottom_mid, bottom_right
tab_pos: 'top_mid'
TabbedPanelItem:
id: delft
text: "Anasayfa"
BoxLayout:
Label:
id: saat
text: "Press the Button =>"
Label:
id: tarih
text: "Press the Button =>"
TabbedPanelItem:
text: "Metalar"
on_release:
if inner_meta.text == "Empty" : Factory.MyPopup().open()
TabbedPanel:
do_default_tab: False
#orantısal küçültür
size_hint: .99, .99
#ekranın ortasına bütün kareyi koyar
pos_hint: {'center_x': .5, 'center_y': .5}
# tabları ortalar - bottom tabları alta atar left_top, left_mid, left_bottom, top_left, top_mid, top_right,
#right_top, right_mid, right_bottom, bottom_left, bottom_mid, bottom_right
tab_pos: 'left_mid'
TabbedPanelItem:
id: inner_meta
text:"Empty"
TabbedPanelItem:
text: "Tab 3"
Image:
source: "/charac.png"
【问题讨论】: