我正在使用Tkinter创建一个简单的GUI应用程序。我的代码如下。行和列似乎没有任何影响。第0列显示在屏幕中央。如果将“行”设置为1,则控件仍显示在“行0”上。
/*Example of Scroll to a specific position in scrollview*/
import React, { Component } from 'react';
//import react in our project
import {
View,
ScrollView,
StyleSheet,
Text,
TouchableOpacity,
Image,
} from 'react-native';
//import all the components we needed
export default class App extends Component {
constructor() {
super();
//Array of Item to add in Scrollview
this.items = [
'Goa',
'Gujrat',
'Madhya Pradesh',
'Assam',
'Gujrat',
'Karnataka',
'Jharkhand',
'Himachal Pradesh',
'Manipur',
'Meghalaya',
'Mizoram',
'Uttarakhand',
'West Bengal',
'Tamil Nadu ',
'Punjab',
'Rajasthan',
'Bihar',
'Andhra Pradesh',
'Arunachal Pradesh',
];
//Blank array to store the location of each item
this.arr = [];
}
downButtonHandler = () => {
// To Scroll to the index 5 element
this.scrollview_ref.scrollTo({ x: 0, y: this.arr[5], animated: true });
};
render() {
return (
<View style={styles.container}>
<ScrollView
ref={ref => {
this.scrollview_ref = ref;
}}>
{/*Loop of JS which is like foreach loop*/}
{this.items.map((item, key) => (
//key is the index of the array
//item is the single item of the array
<View
key={key}
style={styles.item}
onLayout={event => {
const layout = event.nativeEvent.layout;
this.arr[key] = layout.y;
console.log('height:', layout.height);
console.log('width:', layout.width);
console.log('x:', layout.x);
console.log('y:', layout.y);
}}>
<Text style={styles.text}>
{key}. {item}
</Text>
<View style={styles.separator} />
</View>
))}
</ScrollView>
<TouchableOpacity
activeOpacity={0.5}
onPress={this.downButtonHandler}
style={styles.downButton}>
<Text>Go to index 5</Text>
</TouchableOpacity>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
paddingTop: 30,
},
separator: {
height: 1,
backgroundColor: '#707080',
width: '100%',
},
text: {
fontSize: 16,
color: '#606070',
padding: 10,
},
downButton: {
position: 'absolute',
padding: 15,
alignItems: 'center',
justifyContent: 'center',
right: 30,
backgroundColor: '#e8e3e3',
bottom: 70,
},
});
已编辑
确定了问题。原始代码在Visual Studio中。我在Spyder上复制了相同内容,并且坐标工作正常。
VS 2019可能有什么问题?
答案 0 :(得分:1)
据我所知,tkinter
会将您的最低行/列设置为0,并将其用作参考点。您可能需要另一个小部件进行比较,但是在我的测试中,下面的两个示例将产生相同的精确GUI。
import tkinter
window = tkinter.Tk()
window.title("Sample App")
window.geometry('640x480')
tkinter.Label(window, width="5", text="Title").grid(row=0, column=0)
tkinter.Label(window, width="5", text="Title1").grid(row=1, column=1)
window.mainloop()
和
import tkinter
window = tkinter.Tk()
window.title("Sample App")
window.geometry('640x480')
tkinter.Label(window, width="5", text="Title").grid(row=5, column=5) # or even 50000, 50000
tkinter.Label(window, width="5", text="Title1").grid(row=6, column=6) # and 50001, 50001
window.mainloop()
如果您深入到.grid()
源代码,那么我会解释为什么会出现此行为的答案。为了回答您的问题,这些控件工作正常,但是只有一个小部件,即使您指定了(100000,100000),它也始终会在屏幕的左上方(0,0)处将其网格化因为这种将原点定义为指定的最小行/列的行为。据我所知,您将需要一个(0,0)上的小部件和一个(100000,100000)上的小部件,这样才能正常工作!