我在包含Kivy的代码中遇到了错误

时间:2020-08-31 13:25:45

标签: python kivy

from kivymd.app import MDApp
from kivymd.uix.screen import Screen
from kivymd.uix.button import MDFlatButton

class DemoApp(MDApp):

    def build(self):
        screen = Screen()
        btn_flat = MDFlatButton(text='Hello World')
        screen.add_widget(btn_flat)
        return btn_flat


DemoApp().run()

我认为我导入了错误的模块或其他内容。

这是我第一次使用Kivy。

运行代码时,它将打开窗口,但立即关闭窗口。

之后,它给了我这个错误。

感谢所有StackOverflow社区

下面是错误

 Traceback (most recent call last):
   File "C:/Users/Toshiba/Desktop/python_temelleri/Kivy.py", line 14, in <module>
     DemoApp().run()
   File "C:\Users\Toshiba\Desktop\python_temelleri\venv\lib\site-packages\kivy\app.py", line 837, in run
     Window.add_widget(self.root)
   File "C:\Users\Toshiba\Desktop\python_temelleri\venv\lib\site-packages\kivy\core\window\__init__.py", line 1297, in add_widget
     (widget, widget.parent)
 kivy.uix.widget.WidgetException: Cannot add <kivymd.uix.button.MDFlatButton object at 0x000000296765DF98> to window, it already has a parent <Screen name=''>

1 个答案:

答案 0 :(得分:2)

问题是,奇异的import React, { Fragment, useState, useEffect } from 'react'; import Title from './components/Title/Title'; import Content from './components/Content/Content'; import 'bootstrap/dist/css/bootstrap.min.css'; import { Card, Navbar, Form, FormControl, Button } from 'react-bootstrap'; const App = (props) => { const [error, setError] = useState(null); const [isLoaded, setisLoaded] = useState(false); const [articles, setArticles] = useState([]); const [country, setCountry] = useState('gb'); const handleSubmit = (event) => { event.preventDefault(); changeCountryHandler(); }; const changeCountryHandler = (event) => { setCountry(event.target.value); }; useEffect(() => { let url = 'https://cors-anywhere.herokuapp.com/http://newsapi.org/v2/top-headlines?country=' + country; fetch(url, { method: 'GET', headers: { Accept: 'application/json, text/plain, */*', 'Content-type': 'application/json', 'x-api-key': 'myApiKey', SameSite: 'None', }, }) .then((res) => res.json()) .then((result) => { setisLoaded(true), setArticles(result.articles); }), (error) => { setisLoaded(true); setError(error); }; }); if (error) { return <div>Error: {error.message}</div>; } else if (!isLoaded) { return <div>Loading...</div>; } else { return ( <> <Navbar className="bg-secondary justify-content-between" variant="dark"> <p>The selected country is:{country}</p> <Form inline onSubmit={handleSubmit}> <FormControl type="text" placeholder="Search by country" className=" mr-sm-2" onChange={changeCountryHandler} value={country} /> <Button type="submit">Submit</Button> </Form> </Navbar> {articles.map((article) => ( <Card style={{ width: '100%', height: 'auto' }} key={article.title} className="mb-5 mt-4 bg-light" > <Card.Img variant="top" src={article.urlToImage} className="pr-3 pl-3" alt="" /> <Card.Body> <Card.Title> {article.author} | {article.publishedAt} <Title title={article.title}>{article.title}</Title> </Card.Title> Description: {article.description} <Content content={article.content}>{article.content}</Content> Read the full article on{' '} <Card.Link href={article.url} target="_blank" rel="noopener noreferrer" > {article.url} </Card.Link> </Card.Body> </Card> ))} </> ); } }; export default App; 代码可以做到:

App

其中 Window.add_widget(self.root) 是您的self.root方法返回的值。因此,如果您的build()方法可以做到

build()

screen.add_widget(btn_flat)

然后,由于return btn_flat 已经是App的子代,因此,来自奇异鸟btn_flat的上述代码将出现错误。您的screen方法可能应该是:

build()