Мой основной python файл, в который я хочу импортировать тест:
# main.py
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import ObjectProperty
from kivy.uix.popup import Popup
from kivy.uix.label import Label
from database import DataBase
class CreateAccountWindow(Screen):
namee = ObjectProperty(None)
email = ObjectProperty(None)
password = ObjectProperty(None)
def submit(self):
if self.namee.text != "" and self.email.text != "" and self.email.text.count("@") == 1 and self.email.text.count(".") > 0:
if self.password != "":
db.add_user(self.email.text, self.password.text, self.namee.text)
self.reset()
sm.current = "login"
else:
invalidForm()
else:
invalidForm()
def login(self):
self.reset()
sm.current = "login"
def reset(self):
self.email.text = ""
self.password.text = ""
self.namee.text = ""
class LoginWindow(Screen):
email = ObjectProperty(None)
password = ObjectProperty(None)
def loginBtn(self):
if db.validate(self.email.text, self.password.text):
MainWindow.current = self.email.text
self.reset()
sm.current = "main"
else:
invalidLogin()
def createBtn(self):
self.reset()
sm.current = "create"
def reset(self):
self.email.text = ""
self.password.text = ""
class MainWindow(Screen):
n = ObjectProperty(None)
created = ObjectProperty(None)
email = ObjectProperty(None)
current = ""
def Proceed(self):
sm.current = "Home"
def on_enter(self, *args):
password, name, created = db.get_user(self.current)
self.n.text = "Account Name: " + name
self.email.text = "Email: " + self.current
self.created.text = "Created On: " + created
class HomeWindow(Screen):
current = ""
def logOut(self):
sm.current = "login"
class PapersWindow(Screen):
current = ""
def Chemistry(self):
sm.current = "chemistry"
def logOut(self):
sm.current = "login"
class ChemistryWindow(Screen):
current = ""
def next(self):
sm.current = "this"
from app import Question
class NineteenWindow(Screen):
pass
class WindowManager(ScreenManager):
pass
class WindowManager(ScreenManager):
pass
def invalidLogin():
pop = Popup(title='Invalid Login',
content=Label(text='Invalid username or password.'),
size_hint=(None, None), size=(400, 400))
pop.open()
def invalidForm():
pop = Popup(title='Invalid Form',
content=Label(text='Please fill in all inputs with valid information.'),
size_hint=(None, None), size=(400, 400))
pop.open()
kv = Builder.load_file("my.kv")
sm = WindowManager()
db = DataBase("users.txt")
screens = [LoginWindow(name="login"), CreateAccountWindow(name="create"), MainWindow(name="main"), HomeWindow(name="Home"), PapersWindow(name="papers"), ChemistryWindow(name="chemistry"), NineteenWindow(name="this")]
for screen in screens:
sm.add_widget(screen)
sm.current = "login"
class MyMainApp(App):
def build(self):
return sm
if __name__ == "__main__":
MyMainApp().run()
В файле kv у меня есть
<CreateAccountWindow>:
name: "create"
namee: namee
email: email
password: passw
FloatLayout:
cols: 1
FloatLayout:
size: root.width, root.height/2
Label:
text: "Create an Account"
size_hint: 0.8, 0.2
pos_hint: {"x":0.1, "top":1}
font_size: (root.width**2 + root.height**2) / 14**4
Label:
size_hint: 0.5,0.12
pos_hint: {"x":0, "top":0.8}
text: "Name: "
font_size: (root.width**2 + root.height**2) / 14**4
TextInput:
pos_hint: {"x":0.5, "top":0.8}
size_hint: 0.4, 0.12
id: namee
multiline: False
font_size: (root.width**2 + root.height**2) / 14**4
Label:
size_hint: 0.5,0.12
pos_hint: {"x":0, "top":0.8-0.13}
text: "Email: "
font_size: (root.width**2 + root.height**2) / 14**4
TextInput:
pos_hint: {"x":0.5, "top":0.8-0.13}
size_hint: 0.4, 0.12
id: email
multiline: False
font_size: (root.width**2 + root.height**2) / 14**4
Label:
size_hint: 0.5,0.12
pos_hint: {"x":0, "top":0.8-0.13*2}
text: "Password: "
font_size: (root.width**2 + root.height**2) / 14**4
TextInput:
pos_hint: {"x":0.5, "top":0.8-0.13*2}
size_hint: 0.4, 0.12
id: passw
multiline: False
password: True
font_size: (root.width**2 + root.height**2) / 14**4
Button:
pos_hint:{"x":0.3,"y":0.25}
size_hint: 0.4, 0.1
font_size: (root.width**2 + root.height**2) / 17**4
text: "Already have an Account? Log In"
on_release:
root.manager.transition.direction = "left"
root.login()
Button:
pos_hint:{"x":0.2,"y":0.05}
size_hint: 0.6, 0.15
text: "Submit"
font_size: (root.width**2 + root.height**2) / 14**4
on_release:
root.manager.transition.direction = "left"
root.submit()
<LoginWindow>:
name: "login"
email: email
password: password
FloatLayout:
Label:
text:"Email: "
font_size: (root.width**2 + root.height**2) / 13**4
pos_hint: {"x":0.1, "top":0.9}
size_hint: 0.35, 0.15
TextInput:
id: email
font_size: (root.width**2 + root.height**2) / 13**4
multiline: False
pos_hint: {"x": 0.45 , "top":0.9}
size_hint: 0.4, 0.15
Label:
text:"Password: "
font_size: (root.width**2 + root.height**2) / 13**4
pos_hint: {"x":0.1, "top":0.7}
size_hint: 0.35, 0.15
TextInput:
id: password
font_size: (root.width**2 + root.height**2) / 13**4
multiline: False
password: True
pos_hint: {"x": 0.45, "top":0.7}
size_hint: 0.4, 0.15
Button:
pos_hint:{"x":0.2,"y":0.05}
size_hint: 0.6, 0.2
font_size: (root.width**2 + root.height**2) / 13**4
text: "Login"
on_release:
root.manager.transition.direction = "up"
root.loginBtn()
Button:
pos_hint:{"x":0.3,"y":0.3}
size_hint: 0.4, 0.1
font_size: (root.width**2 + root.height**2) / 17**4
text: "Don't have an Account? Create One"
on_release:
root.manager.transition.direction = "right"
root.createBtn()
<MainWindow>:
n: n
email: email
created:created
FloatLayout:
Label:
id: n
pos_hint:{"x": 0.1, "top":0.9}
size_hint:0.8, 0.2
text: "Account Name: "
Label:
id: email
pos_hint:{"x": 0.1, "top":0.7}
size_hint:0.8, 0.2
text: "Email: "
Label:
id: created
pos_hint:{"x": 0.1, "top":0.5}
size_hint:0.8, 0.2
text: "Created: "
Button:
pos_hint:{"x":0.2, "y": 0.1}
size_hint:0.6,0.2
text: "Proceed"
on_release:
app.root.current = "Home"
root.manager.transition.direction = "down"
<HomeWindow>
name: "Home"
FloatLayout:
Button:
pos_hint:{"x":0.5, "y":0.5}
size_hint:0.4,0.6
text: "Pastpapers"
on_release:
app.root.current = "papers"
root.manager.transition.direction = "down"
Button:
pos_hint:{"x":0.00, "y":0.8}
size_hint:0.1,0.1
text: "Log Out"
on_release:
app.root.current = "login"
root.manager.transition.direction = "down"
<PapersWindow>
name: "papers"
FloatLayout:
Button:
pos_hint:{"x":0.5, "y":0.5}
size_hint:0.4,0.6
text: "Chemistry"
on_release:
app.root.current = "chemistry"
root.manager.transition.direction = "down"
Button:
pos_hint:{"x":0.00, "y":0.8}
size_hint:0.1,0.1
text: "Log Out"
on_release:
app.root.current = "login"
root.manager.transition.direction = "down"
<ChemistryWindow>
name: "chemistry"
FloatLayout:
Button:
pos_hint:{"x":0.5, "y":0.5}
size_hint:0.4,0.6
text: "next"
on_release:
app.root.current = "this"
root.manager.transition.direction = "down"
<NineteenWindow>
name: "this"
WindowManager:
LoginWindow
MainWindow
HomeWindow
В первом файле python Викторины : (Question.py)
class Question():
def __init__(self, prompt, answer):
self.prompt = prompt
self.answer = answer
Второй python файл для теста: (app.py)
from Question import Question
question_prompts = [
"What color is an egg?\n(a) White\n(b) Purple\n(c) Orange\n\n",
"What comes after b?\n(a) A\n(b) V\n(c) C\n\n",
"Which of these is a color?\n(a) Teal\n(b) Magneta\n(c) Yellow\n\n"
]
questions = [
Question(question_prompts[0], "a"),
Question(question_prompts[1], "c"),
Question(question_prompts[2], "c"),
]
def run_test(questions):
score = 0
for question in questions:
answer = input(question.prompt)
if answer == question.answer:
score += 1
print("You got " + str(score) + "/" + str(len(questions)) + "correct")
run_test(questions)
Теперь я хочу поместить тест в NineteenWindow. Когда я попытался импортировать тест, как вы можете видеть в классе NineteenWindow в main.py, а затем запустил код, я получил тест в терминале запуска, как показано на рисунке, и затем код перестал работать дальше. Я хочу, чтобы он был виден на экранах, по которым я могу перемещаться, нажимая кнопки.