如何将pyqt5 QLineEdit的输入存储到字符串中

时间:2017-05-08 16:58:23

标签: python-3.x pyqt5

首先,说我还在学习。 所以我搜索了一会儿,仍然不知道该怎么做。 我想使用QLineEdit输入从另一个文件执行另一个函数,该文件从十进制到二进制,从二进制到十进制,所以我想要的是存储该输入,然后在GUI处执行函数和tehn show结果。

#!/usr/bin/env python3
import sys, os
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import binarytodecimal
if __name__ == "__main__":
# Create an PyQT5 application object.
a = QApplication(sys.argv)

# The QWidget widget is the base class of all user interface objects in PyQt4.
w = QWidget()

# Set window size.
w.resize(320, 240)

# Set window title
w.setWindowTitle("GUI!")

btn = QPushButton('Hello World!', w)
btn.setToolTip('Click to quit!')
btn.clicked.connect(exit)
btn.resize(btn.sizeHint())
btn.move(100, 100)

 # Create textbox
textbox = QLineEdit(w)
textbox.move(20, 20)
textbox.resize(280,40)

textbox.textChanged.connect(binarytodecimal.decimal()) #not workking ¿?¿?¿? how to do it?



w.show()

sys.exit(a.exec_())

然后是带有函数的文件:

# To choose a converter you need to write the function decimal(num) or binary(num)


# DECIMAL SIDE
def decimal(decimalin):
    a = True #This mades work the while
    rest ="" #needed to work
    while not a == False:
        rest = str(int(decimalin % 2)) + rest #here I grab the rest of the division
        decimalin= decimalin / 2  #here we made the division
        if decimalin < 1:
            a = False #stops the while
    print("This is your decimal to binary result " + rest ) #result output

# BINARY SIDE

def binary(binaryin):
    binaryin = str(binaryin)
    i=0
    i = len(binaryin) -1
    binarynum = 0
    binarydecimal = 0
    binaryintra = ""
    #
    # The next While turns arround the input
    # Example: if you introduce a 1010 to make easy to use the string and numbers
    # I rotate it to 0101 trying to learn how to make it less botched XDDD
    #
    while i>= 0:
        binaryintra = binaryintra + binaryin[i]
        i = i -1
    #
    # Here I made the binary to decimal, as you can see i use the normal used formula
    # (1, 0) * 2^X 
    # X is for the possision of the binary number
    while not binarynum == len(binaryin):

##        print(binarynum)        #test
        binarydecimal1=int(binaryintra[binarynum])*2**binarynum
##        print(binarydecimal1) #test
        binarydecimal=binarydecimal+binarydecimal1
        binarynum = binarynum +1
    print("This is your binary to decimal result " + str(binarydecimal))

1 个答案:

答案 0 :(得分:1)

您正在将TextBox的textChanged信号连接到函数decimal,而不是调用它的结果:

  

textbox.textChanged.connect(binarytodecimal.decimal())

如果删除小数后的括号,它应该有效:

  

textbox.textChanged.connect(binarytodecimal.decimal)

将信号与功能连接起来。