Python Forum
what is wrong with my code?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
what is wrong with my code?
#11
(Apr-27-2022, 12:31 AM)53535 Wrote: i made a ui , and converted it into code and add two lines

Why creating an ui? The QtDesigner generates code that you would never write like this.
A menu does not need a setGeometry.

You can also add the two lines for the menu like this.

from PyQt5 import QtCore, QtGui, QtWidgets
import sqlite3
from PyQt5.QtWidgets import QMainWindow, QApplication, QFileDialog, QLabel, QAction
import os
 
class MyWindow(QMainWindow):
    def __init__(self):
        super(MyWindow, self).__init__()
        self.setGeometry(100, 100, 400, 300)
        fileMenu = self.menuBar().addMenu('Open')
        fileMenu.addAction(QAction("open sqlite file", self, triggered = self.get_file_name))
         
         
    def get_file_name(self):
        file_filter= "data file (*.sqlite *.sqlite3 *.db)"
        fname,_ = QFileDialog.getOpenFileName(
            parent=None,
            caption="select a file",
            directory=os.getcwd(),
            filter=file_filter
        )
        if fname:
            print(fname)
        else:
            print("cancelled")
 
if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    window = MyWindow()
    window.show()
    sys.exit(app.exec_())
Reply
#12
(Apr-27-2022, 08:53 PM)Axel_Erfurt Wrote:
(Apr-27-2022, 12:31 AM)53535 Wrote: i made a ui , and converted it into code and add two lines

Why creating an ui? The QtDesigner generates code that you would never write like this.
A menu does not need a setGeometry.

You can also add the two lines for the menu like this.

from PyQt5 import QtCore, QtGui, QtWidgets
import sqlite3
from PyQt5.QtWidgets import QMainWindow, QApplication, QFileDialog, QLabel, QAction
import os
 
class MyWindow(QMainWindow):
    def __init__(self):
        super(MyWindow, self).__init__()
        self.setGeometry(100, 100, 400, 300)
        fileMenu = self.menuBar().addMenu('Open')
        fileMenu.addAction(QAction("open sqlite file", self, triggered = self.get_file_name))
         
         
    def get_file_name(self):
        file_filter= "data file (*.sqlite *.sqlite3 *.db)"
        fname,_ = QFileDialog.getOpenFileName(
            parent=None,
            caption="select a file",
            directory=os.getcwd(),
            filter=file_filter
        )
        if fname:
            print(fname)
        else:
            print("cancelled")
 
if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    window = MyWindow()
    window.show()
    sys.exit(app.exec_())

thx, this is also a good alternative solution
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  something is wrong with my code 53535 9 2,186 Apr-27-2022, 10:06 AM
Last Post: 53535

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020