Python 如何调用函数并在另一个窗口中打开?

Python 如何调用函数并在另一个窗口中打开?,python,python-3.x,Python,Python 3.x,大家好,我正在创建一个python Noots and Cross游戏,当我调用显示棋盘格式的函数时,我希望在另一个窗口中打开棋盘格式。因此,在一个窗口中,它将有一个程序询问您想去哪里,以及何时我想显示董事会,我希望它在一个单独的窗口中打开 def boardf(): os.system("mode con cols=15 lines=7") print (" ") print (" | | ") print (" ---+---+--- ")

大家好,我正在创建一个python Noots and Cross游戏,当我调用显示棋盘格式的函数时,我希望在另一个窗口中打开棋盘格式。因此,在一个窗口中,它将有一个程序询问您想去哪里,以及何时我想显示董事会,我希望它在一个单独的窗口中打开

def boardf():
    os.system("mode con cols=15 lines=7")
    print (" ")
    print ("    |   |    ")
    print (" ---+---+--- ")
    print ("    |   |    ")
    print (" ---+---+--- ")
    print ("    |   |    ") 

我正在使用IDLE,但这应该足以让球滚动。IDLE是在Windows上下载Python时随Python一起安装的IDE。您可以通过按Windows键并搜索空闲来打开它

这段代码是初步的,但它应该能让您很好地了解如何使用Tkinter库创建新窗口。我在Python
2.x
上读到,您需要导入带有大写字母T的Tkinter,因此如果您使用
3.x
,您可能需要将导入更改为小写字母T

import os, Tkinter as tk

def boardf():
    os.system("mode con cols=15 lines=7")
    print (" ")
    print ("    |   |    ")
    print (" ---+---+--- ")
    print ("    |   |    ")
    print (" ---+---+--- ")
    print ("    |   |    ") 

def play_x():
    print("implement x logic here")

def play_y():
    print("implement y logic here")

def create_window():
    root_window = tk.Tk()
    b_x = tk.Button(root_window, text="x", command=play_x)
    b_y = tk.Button(root_window, text="y", command=play_y)
    b_x.pack()
    b_y.pack()
    root_window.mainloop()

def main():
    boardf()
    create_window()

main()

我正在使用IDLE,但这应该足以让事情顺利进行。IDLE是在Windows上下载Python时随Python一起安装的IDE。您可以通过按Windows键并搜索空闲来打开它

这段代码是初步的,但它应该能让您很好地了解如何使用Tkinter库创建新窗口。我在Python
2.x
上读到,您需要导入带有大写字母T的Tkinter,因此如果您使用
3.x
,您可能需要将导入更改为小写字母T

import os, Tkinter as tk

def boardf():
    os.system("mode con cols=15 lines=7")
    print (" ")
    print ("    |   |    ")
    print (" ---+---+--- ")
    print ("    |   |    ")
    print (" ---+---+--- ")
    print ("    |   |    ") 

def play_x():
    print("implement x logic here")

def play_y():
    print("implement y logic here")

def create_window():
    root_window = tk.Tk()
    b_x = tk.Button(root_window, text="x", command=play_x)
    b_y = tk.Button(root_window, text="y", command=play_y)
    b_x.pack()
    b_y.pack()
    root_window.mainloop()

def main():
    boardf()
    create_window()

main()

您使用的是什么操作系统?程序应该打开的“窗口”是什么?windows是windows命令提示符shell(
cmd.exe
)吗?或者是像Tkinter这样的窗口图形环境?1)Windows 2)cmd.exe作为记录,
os.system
在一个新的shell中打开;一旦完成,shell就会消失(因此尝试配置它是毫无意义的)。如果您想修改当前shell或其他内容(打开一个单独的shell、配置属性等),您通常无法使用特定于操作系统的工具,例如在Windows上,使用
pywin32
。您使用的是什么操作系统?程序应该打开的“窗口”是什么?windows是windows命令提示符shell(
cmd.exe
)吗?或者是像Tkinter这样的窗口图形环境?1)Windows 2)cmd.exe作为记录,
os.system
在一个新的shell中打开;一旦完成,shell就会消失(因此尝试配置它是毫无意义的)。如果要修改当前shell或其他任何内容(打开一个单独的shell、配置属性等),通常需要使用特定于操作系统的工具,例如在Windows上,使用
pywin32