我想要做的是使用tkinter filedialog选择多个文件,然后将这些项添加到列表中.之后,我想使用列表逐个处理每个文件.
#replace.py
import string
def main():
#import tkFileDialog
#import re
#ff = tkFileDialog.askopenfilenames()
#filez = re.findall('{(.*?)}', ff)
import Tkinter,tkFileDialog
root = Tkinter.Tk()
filez = tkFileDialog.askopenfilenames(parent=root,title='Choose a file')
Run Code Online (Sandbox Code Playgroud)
现在,我可以选择多个文件,但我不知道如何将这些文件名添加到列表中.有任何想法吗?
正如标题中所解释的,我得到错误预期onClick监听器是一个函数,而不是类型对象
但我无法理解为什么这不起作用.据我所知,onClick监听器是一个函数.
这是错误来自的CharacterList组件
import React,{Component} from 'react';
import {connect} from 'react-redux';
import {addCharacterById} from '../actions';
import {bindActionCreators} from 'redux';
class CharacterList extends Component{
render(){
//console.log('name : ',this.props.characters[2].name);
return(
<div>
<h3>Characters</h3>
<ul>
{this.props.characters.map((character)=>{
return(<li key={character.id}>{character.name}
<div
onClick={this.props.addCharacterById(character.id)}
>+</div>
</li>);
})}
</ul>
</div>
)
}
}
function mapStateToProps(state){
return {
characters:state
}
}
export default connect(mapStateToProps,{addCharacterById})(CharacterList);
Run Code Online (Sandbox Code Playgroud)
这是动作创作者
export const ADD_CHARACTER='ADD_CHARACTER';
export function addCharacterById(id){
var action ={
type:ADD_CHARACTER,
id
}
return action;
}
Run Code Online (Sandbox Code Playgroud)
那么,你们觉得怎么样?这里有什么问题?
我正在使用React Router v4并尝试实现一项功能,无论用户单击什么路由,他都会转到登录页面,即如果他尚未登录。
但是登录后,他被重定向到他尝试在登录前访问的同一页面
我已经使用以下代码设法对其中的两条路线进行了此操作,但不确定这是否是最佳方法
<Route path="/dashboard" render={(props) => (
auth ?
(<MainDash props={this.props} />)
:
(<Redirect to="/login"/>)
)}
/>
<Route exact path="/customers" render={(props) => (
auth ?
(<MainApp />)
:
(<Redirect to="/login"/>)
)}
/>
<Route exact path="/staff" render={(props) => (
auth ?
(<MainApp />)
:
(<Redirect to="/login"/>)
)}
/>
<Route path="/login" component={Login} />
<Route path="/logout" component={Login} />
Run Code Online (Sandbox Code Playgroud)
现在,尽管这可以正常工作,但我不想为所有不同的路线重复一次相同的代码。所以有更好的方法吗?
现在,注销后,它什么也没显示,我希望它显示登录页面。因此,我也添加了以下几行,以便用户注销后立即显示登录页面。
<Route path="/login" component={Login} />
<Route path="/logout" component={Login} />
Run Code Online (Sandbox Code Playgroud)
但是还有一个问题->尽管注销后它确实显示了登录组件(仍然将路由显示为'/ logout'),但它带我回到了登录表单(这次路由是'/ login'),并且从该路由登录将我带到仪表板。现在,为什么会这样呢?
希望能对此有所帮助,因为我仍在学习React Router
我在使用antd的格式时遇到了困难。我在此表单中有此选择字段,我想从onChange上获取值,但不知如何使其无法正常工作。
说这是我想要的值的项目
<FormItem
{...formItemLayout}
label={fieldLabels.qcategoryid}
validateStatus={categoryError ? "error" : ""}
help={categoryError || ""}
>
{getFieldDecorator("qcategoryid", {
rules: [{ required: true, message: "Please select Category!" }],
onChange: this.handleCategoryChange
})(<Select>{categoryOptions}</Select>)}
</FormItem>
Run Code Online (Sandbox Code Playgroud)
这是categoryOptions
if (this.props.categories) {
categoryOptions = this.props.categories.map(item => (
<Select.Option
key={item.categoryid}
value={item.categoryid}
name={item.categoryname}
>
{item.categoryname}
</Select.Option>
));
}
Run Code Online (Sandbox Code Playgroud)
我想要类别的名称和ID,因此我创建了一个handleCategoryChange,它被称为onChange,并且能够获取所需的字段。
但是,现在看来,我必须在该字段上单击两次以正确选择它。如果我只单击一次,它将显示在控制台中。但是表单上的字段仍然为空。当我再次单击它时,该字段也会显示在表单中。
所以,我在这里做错了什么。是的!这是handleCategoryChange函数
handleCategoryChange = (value, e) => {
console.log("value is : ", value);
console.log("e : ", e);
this.props.form.setFieldsValue({ qcategoryid: value });
this.setState({
categorySelected: value,
categoryname: e.props.name
});
};
Run Code Online (Sandbox Code Playgroud)
只是为了让自己清楚一点。在单击提交之前,我需要这些值。不提交。
我正在使用react
和创建一个待办事项列表,redux
当我更新redux状态数组时,它不会重新呈现,我的状态实际上是一个包含对象的数组,如下所示:
[{index:1,value:'item1',done:false}, {index:2,value:'item2',done:false}]
Run Code Online (Sandbox Code Playgroud)
我想做的是单击,我想将done的值切换为“ true”,但是以某种方式我无法做到这一点。
这是我在减速器中所做的事情:
list.map((item)=>{
if(item.index===index){
item.done=!item.done;
return [...state,list]
}
Run Code Online (Sandbox Code Playgroud)
但是,即使单击切换按钮后更改仍然完成,它也不会重新渲染。
看来我在某种程度上改变了状态。请告诉我我要去哪里错了,我应该怎么做。
您能举一些类似的例子吗?我可以正确更新简单数组的状态,但是对包含objects的数组执行此操作会使我感到困惑。所以,你能举个例子吗?
这是完整的reducer代码:
export default function todoApp(state=[],action){
switch(action.type){
case 'ADD_TODO':
return [...state,action.item];
case 'TOGGLE_TODOS':
const index = action.index;
const list = state;
list.map((item)=>{
if(item.index===index){
item.done=!item.done;
}
return [...state,list];
});
default:
return state;
}
}
Run Code Online (Sandbox Code Playgroud) 通过回调函数将数据从子组件传递到父组件但不知何故它不起作用.我在这做错了什么?将数据从子组件传递到父组件 - 通过回调函数进行响应
https://codepen.io/silentarrowz/pen/GEMQEP?editors=0010
这是代码
class App extends React.Component{
constructor(props){
super(props);
this.state={
input:'this is the input for now'
}
//this.handleInput=this.handleInput.bind(this);
}
handleInput(x){
this.setState({
input:x
});
alert(this.state.input);
}
render(){
return(
<div>
<h1>Passing props from Child to Parent Component</h1>
<Child getInput={this.handleInput} />
here's the input: {this.state.input}
</div>
);
}
}
class Child extends React.Component{
constructor(){
super();
this.state={
text:''
}
}
passingProps(e){
var newInput=e.target.value;
//alert(newInput);
this.setState({
text:newInput
});
this.props.getInput(this.state.text);
}
render(){
return(
<div>
<input type="text" placeholder="please input a name..." onChange={this.passingProps} />
</div>
) …
Run Code Online (Sandbox Code Playgroud) 我在反应中使用画布,并使用画布将 pdf 渲染为图像。
现在,当我获得新数据时,即添加了另一个 pdf 文件,然后又必须为此使用画布。
我不知道如何修复这个错误,或者如何在再次使用之前删除画布甚至清除画布。
这是相关的代码:
pdfLoop = (item,index) => {
var that = this;
PDFJS.getDocument(item).then(function getPdfHelloWorld(pdf) {
//
// Fetch the first page
console.log('url is : ',item);
pdf.getPage(1).then(function getPageHelloWorld(page) {
var scale = 0.5;
var viewport = page.getViewport(scale);
let cref = 'canvas'+index;
let imgref ='img'+index;
console.log('cref no : ',cref);
console.log('img no : ',imgref);
// Prepare canvas using PDF page dimensions
//
var canvas = that.canvasRefs[cref];
//let imagez = that.imageRefs[imgref];
var context = canvas.getContext('2d');
context.globalcompositeoperation = 'source-over';
// …
Run Code Online (Sandbox Code Playgroud) 这是我正在使用的代码
import os
import decimal
from pyPdf import PdfFileReader
path = r"E:\python\Real Python\Real Python\Course materials\Chapter 8\Practice files"
inputFileName = os.path.join(path,"Pride and Prejudice.pdf")
inputFile = PdfFileReader(file(inputFileName,"rb"))
print "Number of pages:", inputFile.getNumPages()
print "Title:", inputFile.getDocumentInfo().title
Run Code Online (Sandbox Code Playgroud)
现在,当我运行此代码时,我收到一个错误:模块'对象'没有属性'Number'
当我运行上面的代码时,我获取了整个输出的屏幕截图,包含错误和所有内容.所以,请看看,让我知道什么是错的?
我有一个 react formik 表单,其中有一个选择字段,例如该字段具有值 A、B、C、D、E、F。
现在说,另一个字段 ChooseSubType 仅在我选择 B 或 D 时才会显示,并且该字段仅在出现时才会是必填字段,而不会在此之前出现。
现在,我该怎么做呢?
这是第一个字段的代码,即选择字段
chooseAlphabet: Yup.string().required('field required'),
chooseSubType : Yup.string().when('chooseAlphabet',
('chooseAlphabet',schema)=>{
console.log('value business : ',chooseAlphabet);
if(chooseAlphabet === "B"||"D"){
return schema;
}else{
return schema.required('field required');
}
}),
Run Code Online (Sandbox Code Playgroud)
但此代码不起作用。
现在,我要在这里进行哪些更改才能使这项工作如我所愿?
我现在正在学习反应.这是与代码的链接 - http://redux.js.org/docs/basics/ExampleTodoList.html
我在理解这部分代码中发生的事情时遇到了一些困难
const Link = ({ active, children, onClick }) => {
if (active) {
return <span>{children}</span>
}
return (
<a
href="#"
onClick={e => {
e.preventDefault()
onClick()
}}
>
{children}
</a>
)
}
Link.propTypes = {
active: PropTypes.bool.isRequired,
children: PropTypes.node.isRequired,
onClick: PropTypes.func.isRequired
}
Run Code Online (Sandbox Code Playgroud)
我最难理解这个片段
return (
<a
href="#"
onClick={e => {
e.preventDefault()
onClick()
}}
>
{children}
</a>
)
}
Run Code Online (Sandbox Code Playgroud)
{children}在这里意味着什么?它有什么作用?
这有什么作用?
children: PropTypes.node.isRequired,
Run Code Online (Sandbox Code Playgroud)
上述行中的节点是什么意思?
reactjs ×8
javascript ×4
python ×2
redux ×2
antd ×1
decimal ×1
file ×1
formik ×1
forms ×1
html5-canvas ×1
module ×1
pypdf ×1
react-redux ×1
react-router ×1
state ×1
tkinter ×1
yup ×1