根据按钮

时间:2017-11-28 23:02:25

标签: python kivy

我想在按钮上放置图像。按钮的大小取决于屏幕的大小。我想根据按钮的大小选择理想图像的大小。按钮在ButtonTools类中定义。凭借我的知识,我只能通过类ButtonTools获得大小。如何获得屏幕尺寸?

from kivy.app import App
from kivy.uix.widget import Widget

from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button

from kivy.uix.image import Image


class Paint(Widget):
    pass

class ButtonTools(BoxLayout):
    def __init__(self, **kwargs):
        super(ButtonTools, self).__init__(**kwargs)

        self.size_hint = (1, None)
        self.height = 50


        but = Button(on_press = self.DrawAbscissa)
        but.background_normal = 'abscissa.png'
        self.add_widget(but)

        but = Button( on_press = self.DrawCurve)
        but.background_normal ='curve.png'
        self.add_widget(but)


    def DrawAbscissa(self, obj):
        size = self.parent.size
        p=1
        pass

    def DrawCurve(self, obj):
        pass

class WorkShop(BoxLayout):
    def __init__(self, **kwargs):
        super(WorkShop, self).__init__(**kwargs)
        self.orientation = "vertical"

        self.paint = Paint()

        self.tools = ButtonTools()

        self.add_widget(self.paint)
        self.add_widget(self.tools)

class MyPaintApp(App):

    def build(self):
        return WorkShop()


if __name__ == '__main__':
    MyPaintApp().run()

1 个答案:

答案 0 :(得分:1)

您可以使用以下方法获取屏幕尺寸:

public class MainPageViewModel : INotifyPropertyChanged
{
    public ICommand AddNoteIconCommand { get; private set; }
    private SQLiteAsyncConnection _db;
    private ObservableCollection<Note> _notes = new ObservableCollection<Note>();
    public ObservableCollection<Note> Notes            
    {
        get { return _notes; }
        set
        {
            if (_notes == value)
                return;

            _notes = value;
            OnPropertyChanged();
        }
    }


    private INavigation Navigation;

    public event PropertyChangedEventHandler PropertyChanged;

    public MainPageViewModel(INavigation Navigation)
    {
        this.Navigation = Navigation;
        AddNoteIconCommand = new Command(AddNewNote);
        PrepareDatabase();

    }

    private async void PrepareDatabase()
    {
        _db = DependencyService.Get<ISQLiteDb>().GetConnection();
        var _databasenotes = await _db.Table<Note>().ToListAsync();
        FillObservable(_databasenotes);
    }

    private async void AddNewNote()
    {
        await Navigation.PushAsync(new NewNotePage());
    }

    private void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    private void FillObservable(List<Note> _dbnotes)
    {
        foreach (var note in _dbnotes)
        {
            _notes.Add(note);
        }
    }
}