使用PyQT调整小部件的大小

时间:2018-08-15 04:11:00

标签: python pyqt5

我是Python和PyQT的初学者。我正在尝试使用QGridLayout制作GUI,但是在放置图像时遇到了一些问题。这是没有图像的样子:

没有图片的图片

对我很好。但是,当我尝试在右上角添加图像时,我得到:

带有图片的图片

显然不是我想要的(我希望小部件的该部分与第一张图片的大小相同,即大约与原始标题的大小相同)。代码有点长,所以我只发布了网格的一部分:

    wid = QWidget(self)
    self.setCentralWidget(wid)
    grid = QGridLayout()
    title = QLabel(self)
    newfont = QFont("Times", 20, QFont.Bold)
    title.setText('Interfaz Gráfica PIC18F4550')
    title.setFont(newfont)
    title.setAlignment(Qt.AlignCenter)

    pic = QLabel(self)
    pic.setPixmap(QtGui.QPixmap('escudo.gif'))

    m = PlotCanvas(self, width=5, height=4)
    connect_btn = QPushButton('Conectar', self)
    connect_btn.setIcon(QIcon('lautaro.jpeg'))
    connect_btn.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Expanding)
    exit_btn = QPushButton('Salir', self)
    exit_btn.setIcon(QIcon('lautaro.jpeg'))
    exit_btn.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Expanding)
    disconnect_btn = QPushButton('Desconectar', self)
    disconnect_btn.setIcon(QIcon('lautaro.jpeg'))
    disconnect_btn.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Expanding)
    frequency_btn = QPushButton('Frecuencia', self)
    frequency_btn.setIcon(QIcon('lautaro.jpeg'))
    frequency_btn.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Expanding)
    duty_cycle_btn = QPushButton('Duty Cycle', self)
    duty_cycle_btn.setIcon(QIcon('lautaro.jpeg'))
    duty_cycle_btn.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Expanding)
    grid.addWidget(connect_btn, 1, 0, 2, 1)
    grid.addWidget(disconnect_btn, 3, 0, 2 ,1)
    grid.addWidget(exit_btn, 5, 0, 2, 1)
    grid.addWidget(m, 1, 1, 6, 1)
    grid.addWidget(frequency_btn, 1, 2, 3, 1)
    grid.addWidget(duty_cycle_btn, 4, 2, 3, 1)
    grid.addWidget(title, 0, 1)
    grid.addWidget(pic, 0, 2)
    wid.setLayout(grid)

所以,问题是:如何设置小部件尺寸?显然,在这种情况下,与标题和标签相关的小部件比按钮和图形大得多。

1 个答案:

答案 0 :(得分:0)

如果您希望图像始终具有相同的尺寸,则可以如下设置固定的宽度和高度:

@Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_weather_app, container,     false);

    mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    mRecyclerView.addItemDecoration(new DividerItemDecoration(mRecyclerView.getContext(), DividerItemDecoration.VERTICAL));

    requestJsonObject();

    return view;


}


private void requestJsonObject (){

}

如果您希望图像具有最大尺寸,但是如果窗口变小,则应调整大小并变小,您可以使用以下方法:

public class RecyclerViewAdapter  extends         
RecyclerView.Adapter<RecyclerViewAdapter.CurrentRecycler> {

List<Weather> mCurrentWeatherDataList;

public static class CurrentRecycler extends RecyclerView.ViewHolder{

public TextView location;
public TextView currentTemp;
public TextView currentHumidity;
public TextView currentDescription;
public ImageView currentIcon;

public CurrentRecycler (View view) {
    super (view);

    location =  (TextView) view.findViewById(R.id.current_city_location);
    currentTemp = (TextView) view.findViewById(R.id.current_temperature);
    currentHumidity = (TextView) view.findViewById(R.id.current_humidity);
    currentDescription = (TextView) view.findViewById(R.id.current_weather_description);
    currentIcon = (ImageView) view.findViewById(R.id.current_weather_icon);

}

}

public RecyclerViewAdapter (List<Weather> mCurrentWeatherDataList) {
    this.mCurrentWeatherDataList = mCurrentWeatherDataList;
}

@Override
public CurrentRecycler onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item, parent, false);

        final CurrentRecycler  currentRecycler = new CurrentRecycler(view);

        return currentRecycler;
}

@Override
public void onBindViewHolder( CurrentRecycler holder, int position) {

    final Weather currentRecycler = mCurrentWeatherDataList.get(position);
    holder.location.setText(currentRecycler.getDefaultLocation());
    holder.currentTemp.setText((currentRecycler.getDefaultCurrentTemp()));
    holder.currentHumidity.setText(currentRecycler.getDefaultHumidity());
    holder.currentDescription.setText(currentRecycler.getDefaultDescription());


}

@Override
public int getItemCount() {
    return mCurrentWeatherDataList.size();
}